4

I want to create new sockei.io-client on the server side (with node.js) every time when server gets request. But the code below works only once and then sockei.io-client does't response. Application doesn't stop, no errors.

var http = require("http");


http.createServer(function(request, response) {

var io = require('socket.io-client'); 
localSocket = io.connect('http://localhost:9876/');
localSocket.on('connect', function (data) {
                         response.writeHead(200, {"Content-type": "text/plain"});
                         response.write(data.name);
                         response.end();
                         localSocket.disconnect();
       });


}).listen(8080);

Socket.io server (on localhost:9876) works well - it serves clients with names (which is

 data.name

in the code here).

What is wrong?

UPDATE: The problem is solved by myself: To run multiple connection for socket.io-client need to add connect option {'force new connection': true}, like that:

localSocket = io.connect('http://localhost:9876/', {'force new connection': true});

Thank you!

Ivan Vetrov
  • 61
  • 2
  • 7

1 Answers1

0

For those who found this on google - there is a solution for this right now:

Socket.disconnect() kicks the client (server-side). No chance for the client to stay connected :)

force client disconnect from server with socket.io and nodejs

I think you just have to remove the disconnect() line.

Community
  • 1
  • 1
Joël
  • 1,563
  • 1
  • 18
  • 31
  • 1
    well, i'm aware what disconnect() do. I want to create new socket.io-client and connect to the server every time i receive http request. – Ivan Vetrov Oct 24 '12 at 13:07