How to using socket.io with cocos2d-x

Hi everyone!
I am developing a multiplayer game using cocos2d-x html5 version.
The server are writing in nodejs so my question is how can i using socket.io inside cocos2d-x code?
Can you please provide some line of code or separate class for implements this?
Thanks!:stuck_out_tongue:

1 Like

hi taka,

I have socket.io working successfully on the server side with node.js and on the client side within the cocos2d-html5 framework. An example implementation is in my repo at:

You only need a couple lines of code, instructions are below.

In your html file add the socket.io script, this is served dynamically if you have the socket.io node.js module running, there is no need to actually create a โ€˜socket.io/โ€™ directory

<script src="/socket.io/socket.io.js"></script>

In your cocos2d app add this line (you can place this in your layerโ€™s init, or create a helper class):

this.socket = io.connect('http://path:port');

to verify, add a simple handler:

this.socket.on('connect', function() { console.log('connected!')});

Hope this helps. I also had some issues setting up the newest version of Express with socket.io, if you need help with the server side code the key is here:

this.express = require('express'), this.server = this.express(), this.app = this.server.listen({port number}), this.iolib = require('socket.io');

@ this.io = this.iolib.listen(this.app);@

this.server is your Express server, this.io is the socket.io module, and this.io.sockets would be used for communication

Thanks so much! You are my hero:)

Oh, Chris Hannon, you are also my hero Thank you!

Is this working for socketio 1.0?

Itโ€™s seem not work for scocketio 1.0, Iโ€™m face this issue and stuck at this point.

@hannon235
When do i build code to Android or IOS. this Socket work?