Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0.4] cocos2dx socketIO - Migrating from 0.9 issue #1628

Closed
ghost opened this issue Jun 17, 2014 · 18 comments
Closed

[1.0.4] cocos2dx socketIO - Migrating from 0.9 issue #1628

ghost opened this issue Jun 17, 2014 · 18 comments

Comments

@ghost
Copy link

ghost commented Jun 17, 2014

handshaking issue

I can't find handshake url

0.9 under - http://uri/socket.io/1
1.0.4 - http://uri/socket.io/?EIO=2&transport=polling <----- ??
I just found with packet sniffer

it returns sid and pinginterval timeout with http response code 200
but return value has binary data beginning of value <--- just right?

i don't know exactly what this value is correct.

and then I want connect to websocket but can't find any documents for version 1.0.4

where i connect to?

@hden
Copy link

hden commented Jun 17, 2014

Here: https://github.com/Automattic/engine.io-protocol#urls

Replacement of engine.io/ with socket.io/ take place in here

@ghost
Copy link
Author

ghost commented Jun 18, 2014

@hden
I can't find. poor document
where i connect to websocket server
any examples?

@hden
Copy link

hden commented Jun 18, 2014

Try GET http://HOST:PORT/socket.io/?EIO=3&transport=polling&b64=0

response 200

97:0{"sid":"ZDKctOVp2IY4hwXjAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}

The b64 flag specifies base64 encoding

To perform further action, try GET http://HOST:PORT/socket.io/?EIO=3&transport=polling&b64=0&sid=ZDKctOVp2IY4hwXjAAAA

response 200

2:40

@ghost
Copy link
Author

ghost commented Jun 19, 2014

I tried like below

request

http://HOST:PORT/socket.io/?EIO=3&transport=polling&b64=0

response

97:0{"sid":"ZDKctOVp2IY4hwXjAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}

and transport websocket using sid(session key)

request

http://HOST:PORT/socket.io/?EIO=3&transport=websocket&b64=0&sid=ZDKctOVp2IY4hwXjAAAA

response

{"code":3,"message":"Bad request"}

In 0.9 version of socket.io, websocket url was

http://HOST:PORT/socket.io/1/websocket

In 1.0.4 version, where I connect to?

@hden
Copy link

hden commented Jun 19, 2014

Please do a transport upgrade

Corresponding JavaScript code

@ajslater
Copy link

But there is no way for legacy clients, say... deployed mobile clients to connect with websockets first to a 1.0 socket.io server?

@hden
Copy link

hden commented Jun 19, 2014

Nope according to the spec. Legacy clients does not have corresponding parser in the first place.

@ghost
Copy link
Author

ghost commented Jun 19, 2014

thanks @hden
how to send upgrade transport?
is it mean ping pong?

To perform further action, try GET http://HOST:PORT/socket.io/?EIO=3&transport=polling&b64=0&sid=ZDKctOVp2IY4hwXjAAAA

response 200 
2:40

and what means 2:40 ?

@hden
Copy link

hden commented Jun 19, 2014

It's a engine.io payload [{length: 2, type: 4, data:'0'}], please see the spec for detail.

Official JavaScript implementation can be found here.

@ghost
Copy link
Author

ghost commented Jun 19, 2014

thanks i'll see them too

i'm modifying cocos2d-x source socketIO.cpp

i changed this line

pre << "http://" << _uri << "/socket.io/1";

to

pre << "http://" << _uri << "/socket.io/?EIO=2&transport=polling&b64=0";

like this.

and this block

to

    std::string res = s.str();
    std::string sid;
    int pos = 0;

    // sid
    const char* sid_key = "\"sid\":";
    pos = res.find(sid_key);
    if (pos > 0)
        res.erase(0, pos + strlen(sid_key)+1);
    pos = res.find("\"");
    if (pos > 0)
        _sid = res.substr(0, pos);

    // interval
    res = s.str();
    const char* interval_key = "\"pingInterval\":";
    pos = res.find(interval_key);
    if (pos > 0)
        res.erase(0, pos + strlen(interval_key));
    pos = res.find(",");
    pos = MIN(pos, res.find("}"));
    if (pos > 0 )
        _heartbeat = atoi(res.substr(0, pos).c_str()) / 1000;

    // timeout
    res = s.str();
    const char* timeout_key = "\"pingTimeout\":";
    pos = res.find(timeout_key);
    if (pos > 0)
        res.erase(0, pos + strlen(timeout_key));
    pos = res.find(",");
    pos = MIN(pos, res.find("}"));
    if (pos > 0 )
        _timeout = atoi(res.substr(0, pos).c_str()) / 1000;

for updated version.

this block parses 97:0{"sid":"ZDKctOVp2IY4hwXjAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}

so then handshake is done.

next call opensocket function

In last version of socket.io

openSocket URL

url was /socket.io/1/websocket

help to change this line

I tried s << _uri << "/socket.io/?EIO=3&transport=websocket&sid=" << _sid;

but it returns error

according to engine.io README document

it may needs to a upgrade transport

README engine.io

how to upgrade transport?

thanks

@hden
Copy link

hden commented Jun 19, 2014

upgrade packet. corresponding code
probe packet. corresponding code

@man2
Copy link

man2 commented Jun 29, 2014

is there any update on this? is it solved yet? really need this for my game :(

@lee-nk
Copy link

lee-nk commented Jul 14, 2014

I'm just starting to look at this, and having the same issues. Some of the problems addressed above are just json parsing problems. Maybe Cocos2d-x would benefit from am integrated json parser.

@man2
Copy link

man2 commented Jul 14, 2014

there is a library in %cocos2d-x-root%/external/json. maybe you could try to look at that

@fpanettieri
Copy link

I'm writting a Unity-Socket.IO plugin so I've been working with the message parsing.

What I've found is that the first digit matches an Engine.IO packet type id. (https://github.com/automattic/engine.io-protocol#packet)

The second digit it's the Socket.IO packet ID.

Using this rule, a packet "40" actually means
4: Engine.IO message
0: Socket.IO connect

Edit:
You can check the encoder & decoder classes on this repository
(https://github.com/fpanettieri/unity-socket.io/tree/master/SocketIO/Scripts/SocketIO)

@okelot
Copy link

okelot commented Jul 26, 2014

is there any solution for connection to Socket.io version 1 from cocos2d-x?

@lee-nk
Copy link

lee-nk commented Jul 27, 2014

FYI there has been discussion of this subject on the Cocos2d-x forum, including a response from Chris Hannon, the author of the SIOClient class:

http://discuss.cocos2d-x.org/t/sioclient-bug-found-in-emit-function/15526

@darrachequesne
Copy link
Member

Closed due to inactivity, please reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants