I am writing a websocket server with socket.io
but don't know why is not working.
This works:
var WebSocketServer = require('ws').Server;
wss = new WebSocketServer({port: 8100});
wss.on('connection', function(ws) {
console.log('New connection!');
ws.on('close', function() {
console.log('Client connection closed!');
});
ws.on('message', function(message) {
});
});
This runs, but my client is not able to connect:
var io = require('socket.io').listen(8100);
io.sockets.on('connection', function (socket) {
console.log('New Connection');
});
Source: Book Professional Node.js
The client is a Javascript basic web browser client that works with any server so please disregard client side issues.
Try setting the transports explicitly to websocket only, since socket.io has fallback options for long-polling and flash sockets.
var io = require('socket.io')(8100, {transports: ['websocket']})
io.sockets.on('connection', function (socket) {
console.log('New Connection');
});