SIOClient Bug found in emit() function

HI,

I’m setting up a simple relay server using Node.js / Socket.io and the SIOClient class that comes with Cocos2d-x. I am using the older version of Socket.io, as it has recently reached version 1.0, and is no longer compatible with the c++ client code.

With the older version my game connects fine to the server, and can send and receive messages, however the emit() function was not working. After a while of inspecting the console, I realised that the data is missing some quote marks in the JSON, making it invalid.

This line:

pre << "5::" << path << ":{\"name\":\"" << eventname << "\",\"args\":" << args << "}";

Has to be changed to this:

pre << "5::" << path << ":{\"name\":\"" << eventname << "\",\"args\":\"" << args << "\"}";

I added these escaped quote marks, and now the function works. I noticed that the function is not even in the api docs, possibly because it is not working as is?

If this class was nice and functional it would definitely make it a lot easier for people to build multiplayer apps with Node.js. Socket.io is so easy to use. If anyone would like to compare notes about using these features I’d be really interested in starting a converstation. :smile:

On a side note: a number of the problems with the SIOClient class seem to relate to the text parsing code. IMO it would be really valuable if JSON parsing could be integrated into Cocos2d-x, as it would make these kinds of parsing task much easier and future proof. I’m currently using the CAJUN library because it has a nice clean api and is easy to use. Games using Socket.io with data in JSON format would need the fastest possible json parser. There are quite a few around but some are a lot more portable than others.

2 Likes

did you manage to get SIOClient working on socket.io 1.0?

for json parsing, there is already a library in external/json. Maybe we could use that?

Hi @siauw. I would like to get it working with version 1.0, but I’m under some time pressure right now for a POC, so I am going to stick with the older version until I have time to look more closely. I’ll share my results when I have something.

I’ll look into that cocos2d-x json parser. If I can I’ll use it to parse the socket data to it’s a bit more robust. The author of the SIOClient class appears to have a related project for sockets, but it uses the Poco framework, which introduces an extra layer or complexity to the system.

It hasn’t been updated for a few months either so I’m not sure of its status.

@Lee @siauw glad to see you are interested, I wrote the extension for cocos2dx as well as the Poco library mentioned above (in fact I abandoned the Poco project for the cocos2dx extension, as the cocos2d guys provide much better tools for a mobile-targeted cross platform framework), but I’ve been busy with other projects and missed that socket.io moved to 1.0

I will take a look at the docs from LearnBoost and check what changes when I get a chance, although I can’t promise it will be before your deadline. also, the json lib wasn’t integrated when I wrote this, so I will take a look at updating the code away from manually generating the strings as well

Is there a bug filed for this? I’m thinking we will need a mechanism for the dev to set which protocol they are using, unless the changes needed for 1.0 are somehow backwards compatible

1 Like

Actually, @Lee, I believe your problem with the old version stems from the fact that I was encoding the args parameter with quotes prior to passing in, whereas you’ve added them to the transmit string instead.

1 Like

HI, @hannon235 I’m really glad to hear that you are still interested in maintaining the SIO classes. They are definitely a really valuable contribution to the community. I’ll help out any way I can.

I think it makes sense to add the quotes to the transmit string, to make it consistent with the “send()” function, so they would both work like this:

client->send("like this");
client->emit("event_name", "also like this");

Without the change the transmitted data looks like this:

5:::{"name":"event_name","args":oops, no quote marks}

…but we need this for valid JSON:

5:::{"name":"event_name","args":"this is what we need"}

I’m sorry, I have not filed a bug report, as I only just joined the forum and am still finding my way around.

Hi,

Do we have a solution yet for connecting to socket.io 1.0 from cocos 2dx?

Thanks for your help,

D Singh

This needs bumping, any updates? Backwards compatibility with older socket version (like 0.9) on top of support for 1.x would be fantastic.

FYI, for anybody interested, Automattic made significant changes to the socket.io handshake process in versions 1.0 and above, they still have yet to document and publish how this works. This means that just about everybody writing a custom client for socket.io is confused, see the following for more info. If you happen to find a custom client working with v1.0+, or what to reverse engineer the javascript client, let us know what you find out:

https://github.com/Automattic/socket.io/issues/1577
https://github.com/Automattic/socket.io-protocol/issues/10

1 Like