Javascript WebSocket不工作
我有以下javascript代码:Javascript WebSocket不工作,javascript,jquery,firefox,websocket,Javascript,Jquery,Firefox,Websocket,我有以下javascript代码: function ConnectWebSocket() { if ("WebSocket" in window) { myWebsocket = new WebSocket("wss://myserver/mychannel"); myWebsocket.onmessage = function(evt) { alert("onmessage"); } myWebsocket.onopen = function
function ConnectWebSocket() {
if ("WebSocket" in window) {
myWebsocket = new WebSocket("wss://myserver/mychannel");
myWebsocket.onmessage = function(evt) {
alert("onmessage");
}
myWebsocket.onopen = function() {
alert("onopen");
myWebsocket.send("msg0");
myWebsocket.send("msg1");
myWebsocket.send("msg2");
}
myWebsocket.onclose = function() {
alert("onclose");
ConnectWebSocket();
}
} else {
// Do something if there is no websockets support
}
}
ConnectWebSocket();
问题是,在Firefox中,发送消息后连接关闭,而由于onclose事件上的命令,连接重新打开。如果我尝试在onopen上只发送一条消息,连接将保持打开状态,但如果我尝试发送多条消息,连接将关闭。这个问题只出现在Firefox中,没有出现在Chrome中,没有出现在IE中,也没有出现在Safari中
有人能帮我吗?在IE或Chrome等其他浏览器中,一旦建立了连接,它就会一直打开,直到我离开页面。我有40.0.3v的Firefox可能是您的
支持
var没有按照您的预期运行。以下代码在FireFox中工作,无需关闭连接:
function ConnectWebSocket() {
if ("WebSocket" in window) {
myWebsocket = new WebSocket("ws://echo.websocket.org/");
myWebsocket.onmessage = function (evt) {
alert("onmessage");
}
myWebsocket.onopen = function () {
alert("onopen");
myWebsocket.send("a test message");
}
myWebsocket.onclose = function () {
alert("onclose");
ConnectWebSocket();
}
} else {
// Do something if there is no websockets support
}
}
ConnectWebSocket();
- 您可以使用来确保WebSocket 在浏览器中正常工作
- 或者(尽管您的问题是FF),您可以使用调试WebSocket
var url = "ws://echo.websocket.org";
if (!window.WebSocket) alert("WebSocket not supported by this browser");
var myWebSocket = {
connect: function () {
var location = url
this._ws = new WebSocket(location);
this._ws.onopen = this._onopen;
this._ws.onmessage = this._onmessage;
this._ws.onclose = this._onclose;
this._ws.onerror = this._onerror;
},
_onopen: function () {
console.debug("WebSocket Connected");
},
_onmessage: function (message) {
console.debug("Message Recieved: " + message.data);
},
_onclose: function () {
console.debug("WebSocket Closed");
kiosk.connect();
},
_onerror: function (e) {
console.debug("Error occured: " + e);
},
_send: function (message) {
console.debug("Message Send: " + message);
if (this._ws) this._ws.send(message);
}
};
myWebSocket.connect();
setInterval(function() {
myWebSocket._send('msg1');
}, 5000);
这里有一个试试看
var WS = window.WebSocket || window.MozWebSocket;
if (WS){
var websocket = new WS("wss://myserver/mychannel");
}
检查这个Hello kakajan,这个链接对我很好,但它是一个自定义操作。我需要它可以设置的代码。可能吗?你能帮我吗?谢谢。WebSockets API(及其底层协议)仍在积极开发中,目前浏览器之间(甚至同一浏览器的不同版本之间)存在许多兼容性问题。我通过查看链接了解到这一点,这里有一些WebSocket示例,您是用哪种语言编写WebSocket的服务器端的?Java?能否尝试在
.onopen
功能之外发送消息?我不确定,但我认为在.onopen
功能中发送消息时会出现问题。同样的问题,在这里你可以看到我的控制台日志:Socket已经打开。状态=1插槽已关闭。状态=3连接到wss://myWebsocketServer/MyChannel 在加载页面时被中断。这3句话每2秒重复一次。@Manel我更新了我的答案,尝试一下,我添加了我在自己项目中使用的websocket示例,还有一个JSFIDLE链接,我在firefox、chrome、safari中尝试过,没有任何问题。你好,kakajan,谢谢你的回答,但恐怕这在firefox上仍然不起作用。您可以使用此url尝试使用相同的代码“wss://pre-ws.sureact.com:443/myChannel“您将看到我正在获取的console.log。请注意,我在“about:config”中有“network.websocket.extensions.permessage deflate;true”。最好的祝愿,ManelI有firefox v29.0.1,我同时运行wss://pre-ws.sureact.com:443/myChannel
和ws://echo.websocket.org
都运行良好,然后我将我的firefox更新为41.0.1,当我运行ws://echo.websocket.org
websockets运行时没有任何问题,但是当我写你的urlwss://pre-ws.sureact.com:443/myChannel
websocket将在特定时间后关闭,我认为websocket后端有问题,或者最新版本的中有问题firefox@Manel你试过了吗?