我看到这个问题很普遍,但是我无法弄清楚如何解决我的问题。 我正在Python中创建服务器,如下所示:
clients = []
server = None
class SimpleWSServer(WebSocket):
def handleConnected(self):
clients.append(self)
def handleClose(self):
clients.remove(self)
def run_server():
global server
server = SimpleWebSocketServer('', 9000, SimpleWSServer,
selectInterval=(1000.0 / 15) / 1000)
server.serveforever()
t=threading.Thread(target=run_server)
t.start()
if( ...)
for client in clients:
msg = json.dumps({'x': cX, 'y': cY})
client.sendMessage(unicode(msg))
然后以这种方式使用Three.js打开WebSocket:
var ws = new WebSocket('ws://192.168.0.15:9000/');
ws.onopen = function() {
console.log('onopen');
};
ws.onmessage = function (event) {
var m = JSON.parse(event.data);
history.push({ x: m.x * 2 - 1, y: -m.y * 2 + 1});
window.alert("X: "+x + " Y "+ y)
// ... rest of the function.
};
本地主机服务器是使用node.js创建的,日志中没有任何错误,与此同时,在Google Chrome浏览器中,我遇到了错误:“ threejs_prova.js:3 WebSocket连接到'ws://192.168.0.15 :9000 /'失败:在收到握手响应之前,连接已关闭”
Firefox中的错误是:
Firefox can't connect to server ws://192.168.0.15:9000/
答案 0 :(得分:0)
以我为例
var ws = new WebSocket('ws://192.168.0.15:9000/');
与
var ws = new WebSocket('ws://localhost:9000/');
解决了问题