Has anybode ever written server with node.js and socket.io?


#1

Hi:
I’m using the v2.1.4 cocos2d-html5.And it begins support websocket.
Then I began to write server through node.js and socket.io.
Before I start , I wrote a simple server as below:
`var app = require(‘http’).createServer(handler);
app.listen(1000);
var io = require(‘socket.io’).listen(app);
//io.set(“destroy upgrade” ,false);
function handler (req, res) {
res.writeHead(200, { ‘Content-Type’: ‘text/html’ });
res.end(‘Hello World\n’);
}

io.sockets.on(‘connection’, function(socket){
console.log(“Connection " + socket.id + " accepted.”);
socket.on(‘message’, function(message){
console.log("Received message: " + message + " - from client " + socket.id);
});
socket.on(‘disconnect’, function(){
console.log(“Connection " + socket.id + " terminated.”);
});
});`

Then I changed the code in WebSocketTest.js(provided in ExtensionTest/NetworkTest).
this._wsiSendText = new WebSocket(“ws://localhost:1000”); //the code I changed*

And when I run this test in chrome, chrome console said it couldn’t connet to server ‘localhost:1000’.
And the cmd of running server said ‘debug-destroying non-socket.io upgrade’.
Why?


#2

Hi S,

the socket.io handshake is a little more complex than simply opening a websocket, there needs to be a negotiation between the two. if you are using cocos2d-html5, you should simply use the socket.io client that is provided by LearnBoost. You can check out some of my repo’s on github (github.com/hannon235) on how to use nodejs + socket.io + cocos2d-html5

I have created a c++ extension for Socket IO, but the javascript bindings do not exist yet. You can see how the negotiation and handshake work by checking out the socket.io source or by checking out the SocektIO extension in the develop branch of cococs2d-x on github