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!