Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one properly shutdown socket.io / websocket-client?

I'm trying to create a test using LearnBoost's socket.io and the node-websocket-client. Communication between the client and server work great. After all communication is done, I close both the client and the server. Yet the program hangs, waiting on some unknown callback. Two questions:

  1. What is the following program waiting for?
  2. Is there a tool for diagnosing outstanding callbacks in node programs?

var connect = require('connect'),
    io = require('socket.io'),
    WebSocket = require('websocket-client').WebSocket;

var port = 7111;

var server = connect.createServer();

var socket = io.listen(server); 
socket.on('connection', function(client) {
  client.send('Welcome!');

  client.on('message', function(message) {
    console.log(message);
  });

  client.on('disconnect', function() {
    console.log('closing');
    server.close();
  });
});

server.listen(port, function() {
  var ws = new WebSocket('ws://localhost:' + port + '/socket.io/websocket');
  ws.onmessage = function(message) {
    console.log(message.data);
  };

  setTimeout(function() {
    ws.send('~m~3~m~Yo!');
    ws.close();
  }, 10);
});

EDIT: changed the variable name of the WebSocket to ws to avoid confusion

like image 498
Chris Avatar asked May 11 '11 14:05

Chris


People also ask

How do I disable a Socket.IO server?

close([callback])​ Closes the socket.io server. The callback argument is optional and will be called when all connections are closed.

Does Socket.IO reconnect after disconnect?

Socket disconnects automatically, reconnects, and disconnects again and form a loop. #918.

How do you check if Socket.IO client is connected or not?

You can check the socket. connected property: var socket = io. connect(); console.


2 Answers

var socket = io.listen(server);

You've created a socket on a port. You've never closed it.

socket.server.close() closes your (socket.io) socket.

When in doubt read the socket.io github examples

socket.server === server It's the server you pass in, in the liste statement so it's closed. I'm not sure what it's waiting for.

like image 100
Raynos Avatar answered Oct 28 '22 21:10

Raynos


Below a way to shutdown all the connections and be able to run multiple expresso tests (using socket.io and socket.io-client).

The solution is tricky and buggy but works on 0.8.5. The main problem is regarding the library to use websockets (node-websocket-client).

Currently, on socket.io, the OS contributors have patched the websocket client. So, we must do the same on our socket.io-client npm package to be able to use finishClose method on the socket client side. Socket.io-client uses the websocket library as npm package, so you must find the file (websocket.js) and substitute it with the same on socket.io.

Afterwards, you could use finishClose method to ensure the connections are closed and with some custom server/client socket settings, the tests will run correctly.

  var  io = require("socket.io").listen(port);

  io.set('close timeout', .2);
  io.set('client store expiration', .2);

  var client = require("socket.io-client").connect( "http://localhost", { port: port ,  'reconnect': false, 'force new connection': true});

  client.on('connect', function() {

        client.disconnect();

  });

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

      client.socket.transport.websocket.finishClose();
      io.server.close();

  });

  io.server.on('close', function() {

      setTimeout( function() {

        done();

      }, 500);
  });

Hope, somebody can help.

like image 3
ppcano Avatar answered Oct 28 '22 23:10

ppcano