Does jsb support socket.io?

as the title says, thanks!

Cpp supports Socket.io, but we haven’t exposed it to JS. Could you finish that ?

Sorry that i don’t know how to bind cpp to js, do you guys plan to expose it in the near future?

Can the Cpp version’s emit function pass in a function as a callback like the js version does?

I also need it , but i haven’t experience with jsb , socketio interface , who can implement that ?
Thanks

@James Chen: I wonder if it’s possible to write a multiplayer game with Cocos2d HTML5 + socket.io completely in javascript and then run it natively with Cocos2d-X?

Would the approach be:

  1. Create a C++ socket.io client and interface that back to the javascript engine, or
  2. Run socket.io within the javascript engine?

Here’s a few examples:


I’m willing to help out on this. :slight_smile:

i think you can use websocket in jsb

Confirmed. You can now use WebSocket in jsb. Tested with Cocos2d-X 2.2.2 iOS. Good job Cocos2d-X team!

It’s possible to run socket.io by mimicking window.location, including some code for setTimeout/clearTimeout and then including socket.io.js. Like this:

// mimic window.location
this.location = "";

// implement timers
require("timers.js");
this.timerLoop = makeWindowTimer(this, function (ms) { /*sleep(ms / 1000);*/ });

// load socket.io
require("socket.io.js");

Get timers.js from here:

Note: I couldn’t figure out how to get timers.js to actually work. You’ll notice the sleep(ms/1000) is commented out above. It seems that if setTimeout/clearTimeout is defined, socket.io is happy. So if anyone knows how to get setTimeout() to function correctly, please share.

Here’s a script that verifies WebSocket is accessible via jsb:

function testWebSocket()
{
    var ws = new WebSocket("ws://echo.websocket.org");

    ws.onopen = function(evt)
    {
        ws.send("Hello WebSocket中文, I'm a text message.");
    };

    ws.onmessage = function(evt)
    {
        cc.log("Test WebSocket response: " + evt.data);
        ws.close();
    };

    ws.onerror = function(evt) {
        cc.log("Test WebSocket error");
    };

    ws.onclose = function(evt) {
        cc.log("Test WebSocket closed");
        ws = null;
    };
}
testWebSocket();

Outputs:

Cocos2d: JS: Test WebSocket response: Hello WebSocket中文, I'm a text message.

Cocos2d: websocket (0xa5b05d0) connection closed by client
Cocos2d: connection closing..
Cocos2d: JS: Test WebSocket closed