sitegui / nodejs-websocket

A node.js module for websocket server and client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

connection.readyState is always 1

cagdasdoner opened this issue · comments

Hi there,
My websock server runs well but when I check readyState of my disconnected devices they are still assigned as 1.

What am I missing to do?

Thanks pal.

Hi @cagdasdoner,

Connection#readyState has four values and the code seems to correctly assign it.

Could you share some code reproducing this behavior you're seeing?

Hi,

I have multiple nodes that connected to this websocket server. But when I powered off one of them, its connection is shown as still alive. Also I was expected to having a connection close event but I do not know if it is defined in RFC or not. (Actually due to TCP conn nature, it might be.)

So, to an easy check, I prepared a code below :

function WSCheckStatus()
{
    var index = 1;
    WSServer.connections.forEach(function (conn) {
        console.log(index + ". state is : " + conn.readyState);
        index ++;
    })
}

var genericTimer = setInterval(WSCheckStatus, 10000);

I also got close event listener for connection like :

    conn.on("close", function (code, reason) {
        connectedWSUsers--;
        console.log("Connection closed. Code : " + code + ", reason : " + reason);
    });

But, I am always getting the ready state as below despite one of the devices powered off :

ubuntu@ip-172-31-40-238:~/iot/websoc$ node websocs.js
Received -> {"origin":"device","type":"ws_init","deviceId":1093699}

  1. state is : 1
  2. state is : 1
    Received -> {"type":"switch","deviceId":"1093699","origin":"userclient"}
  3. state is : 1
  4. state is : 1
  5. state is : 1
  6. state is : 1
  7. state is : 1
  8. state is : 1
  9. state is : 1
  10. state is : 1
  11. state is : 1
  12. state is : 1
    ....

It's probably due to the TCP's way of handling connections.

Have you tried something like socket.setKeepAlive(true, 30e3) and socket.setTimeout(30e3)?
With this, the TCP will send probes (ping/pong packages) every 30s and you can then use the 'timeout' event to close the connection.

If the connection is closed cleanly (ie, with the close() method), this should not happen, since the WebSocket protocol defines a shutdown handshake, to correctly notify both sides the connection is closing.