2

I have the following code in javascript:

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();

The problem is that in Firefox, the connection is closed after sending the messages, and reopened due to the command on the onclose event. If I try to send only one message on onopen, the connection keeps opened, but if I try to send more than one message, the connection shut down. This issue appears only in Firefox, not in Chrome, not in IE, not in Safari.

Can someone help me? In other browsers like IE or Chrome, once the connection is created, it keep opened until I leave the page. I have the 40.0.3v of Firefox

Manel
  • 59
  • 1
  • 1
  • 5
  • check this [http://stackoverflow.com/questions/31627450/websocket-fails-in-firefox](http://stackoverflow.com/questions/31627450/websocket-fails-in-firefox) – kakajan Sep 22 '15 at 16:35
  • Hello kakajan, this link is good for me, but it is a custom action. I need that it could be setted by code. Is it possible?Could you help me? THank you. – Manel Sep 28 '15 at 14:18
  • **The WebSockets API (and the underlying protocol) are still in active development, and there are many compatibility issues across browsers at this time (and even among different releases of the same browser).** I get this from [mozilla's website](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications#Examples) Check the link, there is some WebSocket examples – kakajan Sep 28 '15 at 16:15
  • And in which language did you write the server side of websocket? Java? – kakajan Sep 28 '15 at 16:16
  • Can you try sending messages outside of `.onopen` function? I am not sure, but I think the problem occurs when you send messages in `.onopen` function. Same issue on this [question](http://stackoverflow.com/questions/26204654/websocket-connection-closed-automatically) too – kakajan Sep 28 '15 at 16:22
  • Try my answer and share your console log output – kakajan Sep 28 '15 at 16:28

3 Answers3

3

Try this example:

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);

Here is a JSFidlle

kakajan
  • 2,614
  • 2
  • 22
  • 39
  • Hello kakjan, here you can see my console log: Socket has been opened. State = 1 Socket has been closed. State = 3 Connection to wss://myWebsocketServer/MyChannel was interrupted while the page was loading. And this 3 sentences are repeated every 2 seconds. – Manel Sep 30 '15 at 10:13
  • @Manel i updated my answer, try it, i added my websocket example which i used on my own projects, there is a jsfiddle link too, i tried it in firefox, chrome, safari, works without any problem. – kakajan Sep 30 '15 at 18:35
  • Hello kakajan, thank you for your answer, but I'm afraid that is still not working on firefox. You could try your same code with this url "wss://pre-ws.sureact.com:443/myChannel" and you will see the console.log that I'm obtaining. Please, note that I have "network.websocket.extensions.permessage-deflate;true" at "about:config". BEst regards, Manel – Manel Oct 01 '15 at 09:56
  • I had firefox v29.0.1, i run both `wss://pre-ws.sureact.com:443/myChannel` and `ws://echo.websocket.org`, both works good, then i updated my firefox to 41.0.1, when i run `ws://echo.websocket.org` websockets works without any problem, but when i wrote your url `wss://pre-ws.sureact.com:443/myChannel` websocket is closing after specific time, i think you have problem in backend of websocket or there is problem in latest versions of firefox – kakajan Oct 01 '15 at 11:37
  • @Manel did you try it? – kakajan Oct 03 '15 at 07:32
  • Check your libraries on your backend side of websocket, because as websocket specification change browsers update their implementation, but some libraries on backend maybe use outdated specification. – kakajan Oct 03 '15 at 08:50
  • Hello Kakajan, I'm on holidays nowadays. I'll trry itwhen I return to work. Thank you for your support. I'll inform you. Best regards – Manel Oct 06 '15 at 16:31
0

It may be that your support var is not behaving as you expect. The following code works in FireFox without closing the connection:

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();

Example Fiddle


  • You can use the tool on Websocket.org to make sure websockets are working correctly in your browser.
  • Or (although your issue is with FF) you can use the steps listed here to debug websockets.
Community
  • 1
  • 1
Michael Doye
  • 8,063
  • 5
  • 40
  • 56
0

Try it.

var WS = window.WebSocket || window.MozWebSocket;

if (WS){
    var websocket = new WS("wss://myserver/mychannel");
}
Dominik
  • 6,078
  • 8
  • 37
  • 61
toto
  • 1,180
  • 2
  • 13
  • 30