Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs websocket detect disconnected socket

I have a nodejs websocket server and I have the following problem.

When my clients connect to the server and terminate gracefully the onclose method is called on those sockets and I perform clean up operations on the closed socket.

When the client disconnects due to network, the onclose method is not called. Is there any timeout to be set so onclose is called automatically after a timeout?

I am using ws package for the websocket server in nodejs

like image 476
Pavan K Avatar asked Feb 19 '16 11:02

Pavan K


People also ask

How do I know if a WebSocket is disconnected?

You can check if a WebSocket is disconnected by doing either of the following: Specifying a function to the WebSocket. onclose event handler property, or; Using addEventListener to listen to the close event.

Can you ping a WebSocket?

Pings and Pongs: The Heartbeat of WebSockets At any point after the handshake, either the client or the server can choose to send a ping to the other party.

Can WebSockets be spoofed?

If you build your websocket over HTTP, then yes, it is completely possible for a third party to spoof the connection (and also to eavesdrop). If your HTTPS/WSS system does not properly validate certificates, then that also can be spoofed.


2 Answers

You can check it in the official library documentation. I don't want to copy-paste it here because it can be out of date soon.

like image 185
Alex Misiulia Avatar answered Oct 13 '22 19:10

Alex Misiulia


Well I'll try to answer your question with two examples. Try to analyze both of them and learn how they work. They are both tested and working.

1- Websocket:

Server:

var WebSocketServer = require('websocket').server;
var http = require('http');

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});
server.listen(3000, function() {
    console.log((new Date()) + ' Server is listening on port 3000');
});

wsServer = new WebSocketServer({
    httpServer: server,
    autoAcceptConnections: false
});

function originIsAllowed(origin) {
  return true;
}

wsServer.on('request', function(request) {
    if (!originIsAllowed(request.origin)) {
      request.reject();
      console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
      return;
    }

    var connection = request.accept('echo-protocol', request.origin);
    console.log((new Date()) + ' Connection accepted.');
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            console.log('Received Message: ' + message.utf8Data);
            connection.sendUTF(message.utf8Data);
        }
        else if (message.type === 'binary') {
            console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
            connection.sendBytes(message.binaryData);
        }
    });
    connection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });
});

Client:

<!DOCTYPE html>
<html>
<head>
    <title>Web socket Experiment</title>
    <script type="text/javascript">
        function callWebSocket() {

            var socket = new WebSocket("ws://localhost:3000", 'echo-protocol');

            socket.onopen = function () {
                alert("Hello, Connected To WS server");
            };

            socket.onmessage = function (e) {
                alert("The message received is : " + e.data);
            };
            socket.onerror = function (e) {
                alert("An error occured while connecting... " + e.data);
            };
            socket.onclose = function () {
                alert("hello.. The coonection has been clsoed");
            };

        }
    </script>
</head>

<body>
    <input type="button" value="Open Connecton" onclick="callWebSocket()" />
</body>
</html>

2- Socket.io:

Server:

var http = require('http');
    var app = require('express')();
    var httpServer = http.createServer(app)
    var io = require('socket.io')(httpServer);

app.get('/', function(req, res) {
    res.sendfile(__dirname + '/index.html');
});

io.on('connection', function(socket) {
    socket.emit('news', {
        hello: 'world'
    });
    socket.on('my other event', function(data) {
        console.log(data);
    });
    socket.on('disconnect', function(data) {
        console.log('disconnect!');
    });
});

httpServer.listen(3000);

Client:

<html>
<head>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>
  var conn_options = {
    'sync disconnect on unload':false
  };
  var socket = io.connect('http://localhost:3000',conn_options);
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>
</head>
<body>
</body>
</html>
like image 39
Ehsan Avatar answered Oct 13 '22 17:10

Ehsan