14

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

Chris
  • 1,713
  • 2
  • 12
  • 16
  • 2
    Answer to second question: There is no tool: See http://stackoverflow.com/questions/5916066/node-js-tool-to-see-why-process-is-still-running – 700 Software May 11 '11 at 14:20

3 Answers3

6
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.

Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138
Raynos
  • 166,823
  • 56
  • 351
  • 396
  • This is incorrect. In the example above, I close the server when the client disconnects. The example above is basically a copy of one of the tests formerly in socket.io, specifically `tests/transports.websocket.js`. It looks like Guillermo has been changing a lot of stuff on the trunk, so I'll check out his new testing method to see if they exit more gracefully. – Chris May 11 '11 at 17:22
  • @Chris didn't realise `socket.server` was just the connect server. I thought it was something different. – Raynos May 11 '11 at 19:20
3

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.

ppcano
  • 2,831
  • 1
  • 24
  • 19
0

The program is waiting because socket.io (server) is still listening for incoming connections. I don't know of any way to stop listening.

Detect
  • 2,049
  • 1
  • 12
  • 21
  • The socket.io is listening to connections through the http server. The proper way to shutdown is to close the http server. I've done this in the test and the server shuts down, but the program does not exit. – Chris May 11 '11 at 17:26
  • If you comment out the socket.io listener, the program will stop on `server.close()`. – Detect May 11 '11 at 17:52
  • `/*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(); }); });*/` and then add `server.close()` after `ws.close()` – Detect May 11 '11 at 19:33