2

So when I start the js socket on the website I am receiving this error: enter image description here

and after some errors of 'websocket is closed ....' the website will finally connect without errors, but if I refresh again, then I need to wait for 4-5 or 10 errors until it successfully connect.

Any solutions, please?

I'm using this code for socket server:

var server = require('http').createServer();
var io = require('socket.io')(server);
server.listen(3001);

Frontend code:

function connect()
{
    if (!SOCKET)
    {
        var hash = getCookie('hash');
        SOCKET = io('IP:3001');
        SOCKET.on('connect', function(msg) {
            SOCKET.emit('hash', {
                hash: hash
            });
        });
        SOCKET.on('connect_error', function(msg) {
            $.notify('Connection lost!', 'success');
        });
        SOCKET.on('message', function(msg) {
            onMessage(msg);
        });

        SOCKET.on('disconnect', function() {

        });
    }
    else
    {
        console.log("Error: connection already exists.");
    }
}
Andrei Elvis
  • 43
  • 1
  • 6
  • Possible duplicate of [What does "WebSocket is closed before the connection is established" mean?](https://stackoverflow.com/questions/12487828/what-does-websocket-is-closed-before-the-connection-is-established-mean) – jcaron Nov 15 '17 at 10:14
  • I am not using websocket for javascript, I am using socket.io. – Andrei Elvis Nov 15 '17 at 10:21
  • What do you think `socket.io` uses? The error message is quite explicit... – jcaron Nov 15 '17 at 10:23
  • Please update your post rather than adding code in comments. – jcaron Nov 15 '17 at 10:23
  • Yes, I understand socket.io is websocket, but on that post there is code with ws.close, ws.onopen, ws.onerror, and I don't have those functions because I'm using socket.io... – Andrei Elvis Nov 15 '17 at 10:25
  • It gives you a hint to the fact that the underlying socket is closed, which probably means the higher-level `socket.io` socket is closed as well. It's not clear from your code what the scope of `SOCKET` is. You may want to add a log or a breakpoint to see if you wouldn't be re-opening a new socket. – jcaron Nov 15 '17 at 10:28
  • I found the problem, it was because I was using port `3001`, and I saw on the post you gave me, someone said use port `3000`, now it's fixed, thanks, bro. – Andrei Elvis Nov 15 '17 at 10:30
  • Not sure I understand how that solved the problem given what you posted, but as long as it works for you... – jcaron Nov 15 '17 at 10:31
  • It was short term fixed.... I don't know it worked somehow, but now I have the same problem :( even with port 3000. – Andrei Elvis Nov 15 '17 at 21:27
  • I have the same issue, If I don't refresh for 5-6 minutes, it connects.. If I refresh after, I get this error. It doesn't event get to my back-end. I don't close the connection myself. This is weird. – Andrew Donovan Nov 17 '17 at 01:27
  • Yes... I know it's very annoying. – Andrei Elvis Nov 17 '17 at 19:31

1 Answers1

-1

To fix this issue you need to install the npm package socket.io@1.7.3. Thanks!

Andrei Elvis
  • 43
  • 1
  • 6