我的游戏有问题。我正在 Unity3D 5 和 WebGL 中开发一款游戏。游戏使用网络套接字连接到服务器,其地址如下所示:
wss://serveraddress:8443/moreaddress
它使用 JavaScript 实现来连接到服务器。 JavaScript 看起来像这样:
var WSClient = {
socket: null,
url: null,
connect: function(host) {
if ('WebSocket' in window) {
this.socket = new WebSocket(host);
} else if ('MozWebSocket' in window) {
this.socket = new MozWebSocket(host);
} else {
Console.log('Error: WebSocket is not supported by this browser.');
return;
}
this.socket.onopen = function()
{
SendMessage("WebSocketManager","OnOpenWebSocket");
};
this.socket.onclose = function() {
SendMessage("WebSocketManager","OnCloseWebSocket");
};
this.socket.onmessage = function(message) {
if(typeof message == "string"){
SendMessage('WebSocketManager','OnMessageReceived', message);
}
};
},
initialize: function(url) {
if (typeof url !== "undefined")
this.url = url;
if (this.url == null) {
Console.log('Info: Initialize without an URL');
return;
}
this.connect(this.url);
},
sendMessage: function(msg) {
if (msg != '') {
this.socket.send(msg);
}
},
close: function() {
this.socket.close();
}
};
SendMessage函数只是Unity中调用函数的东西。所以基本上发生在我身上的事情是在我的游戏连接到服务器后,调用 onOpen()
函数,然后从 Unity 调用我的 OnOpenWebSocket()
函数,然后尝试向服务器发送消息进行登录,服务器接收消息并尝试向我发送答案,但我在 onMessage()
函数中收到的只是此消息:{ “isTrusted”:true}。
这发生在 Firefox 上,但在 Chrome 上工作得很好。在 Chrome 上,我从服务器收到了正确的消息。并且服务器中没有任何地方可以写入 isTrusted
。
我使用的是 Firefox 33.0。和Chrome 39.0.2171.95
我查看了firefox的about:config
,websocket
已启用。
有人知道什么可能导致这种情况吗?
最佳答案
我在尝试 websockets 时遇到了同样的问题。我将代码更改为
var ws = new WebSocket("ws://localhost:3331"); // some url
ws.onmessage = function(event) {
console.log(event.data);
}
然后就可以在firefox上运行了! onmessage 回调的类型为 EventListener,并接收 MessageEvent 作为参数,请参阅 https://developer.mozilla.org/en-US/docs/Web/API/WebSocket .
关于javascript - Firefox webgl 上的 Websocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27869491/