Socket.IO Extention Update - 1.x working with 0.9.x backwards compat

I’ve updated the socket.io network extension to properly work with the newest version of socket.io (1.3.5 at this time) as well as maintain backwards compatibility with versions 0.9.x

The pull request is available here for testing: https://github.com/cocos2d/cocos2d-x/pull/11441

This is based on code that I created using the Poco C++ library here: https://github.com/hannon235/socket.io-poco.git
(socket.io 1.x version compatibility was added by https://github.com/francoisTemasys)

A socket.io testserver for both 0.9 and 1.x socket.io can be found here: https://github.com/hannon235/socket.io-testserver.git
(it is pretty bare-bones, be sure to npm install in both of the subdirectories)

Please test / review and let me know if there are any issues

Related Topics:






http://discuss.cocos2d-x.org/t/socketio-incompatibility-9479/18834

5 Likes

Thanks for the great work,

Would you also include a instruction of how to install this extension to the cocos2dx project.

Hi Ryan,

The Socket.IO extension is already build into the cocos2d-x project as well as the test suite. My pull request hasn’t been merged to the main repo, but you can find examples on how to use for both the original and the new version here:

thanks for the response,

however, I am looking for the instruction for installing the new extension “socket.io-poco”

I am able the build it from cmake, but I dont know how to put it to the cocos2dx project

socket.io-poco is a completely separate project that I created using another c++ framework (Poco)

the socket.io extension for cocos2d-x that works with version 1.0 can be found in my pull request or in my branch of the repo here:

Yes, I want to use this separate project (poco) in cocos2dx,

Can you explain how to install it into cocos2dx?

thanks.

1 Like

am completely a beginner to this, and i wanna get started right away…
could you include the server script for the NetworkTest/SocketIOTest.cpp

EDIT: NVM found it…its in OP… thanks :smiley:

@hannon235
ok it works on CocosJs with above code on Socket1.0 with few edits…
by replacing #include “rapidjson/xxx.h” to “json/xxx.h”
and writer.Key to writer.String in SocketIO.Cpp
i am able to connect to the server, but i am unable to emit data to the server. this is the error i get in the client

JSB SocketIO emit event 'echotest' with payload: [{"name":"myname","type":"mytype"}]
JSB SocketIO::SIODelegate->onError method called from native with data: Client not yet connected
JSB SocketIO::SIODelegate->fireEventToScript method called from native with name 'error' data: Client not yet connected

here is how it looks on the client … am i doing anything wrong ?

var socket = SocketIO.connect('http://localhost:7000', {"force new connection" : true});
socket.emit("echotest","[{\"name\":\"myname\",\"type\":\"mytype\"}]");
1 Like

The pull request has not been merged into either the v3 or v4 branches yet, so unless you manually replace the socketio files in the cocos2d-x frameworks with the ones in my pull request or update-sio branch it will not connect with a 1.x server

1 Like

@hannon235 Thanks for the bug fix. Has it already included with 3.8? Because I’m having an issue with SocketIO. I could connect but, couldn’t emit.

So, please kindly share me which files to be updated.

Many thanks in advance.

Edit: I already found SocketIO.cpp & SocketIO.h.
What else do I need to do? Just compile Android?

@eltu Dear… did you manage to solve SocketIO issue? Could you please kindly share me. Thanks in advance.

@StudioAMK yes this was merged into v3.8 and should just compile, is there a certain error or message you are receiving? Or can you share snippets of your client and server code?

Thanks for your reply… I’m been waiting for 2 days for someone to help me…

Please allow me to reinstall 3.8 as I modified here and there and couldn’t rollback.
After that, I will send you the logcat messages and my codes.

Once again thanks

This is the server script. It sends “Hello” upon a new socket connected and has another listener “rpc”.
That “rpc” listener sends “rpc_ret” to client.

Here’s client script

Upon the client connects, it emits a “rpc” message with a data object.


Here’re the test results:
First tested on Browser and resulted as desire (logs from console):

The was from NodeJS. The request from browser was successfully logged.

And then, complied, sync the apk from adb and ran the app.
Here’re the logs from logcat:

There were warnings:

09-17 00:35:13.634: W/dalvikvm(17476): dvmFindClassByName rejecting 'org/cocos2dx/lib/Cocos2dxLocalStorage'
09-17 00:35:43.374: W/dalvikvm(17476): dvmFindClassByName rejecting 'org/cocos2dx/lib/Cocos2dxHttpURLConnection'

Not sure they were the causes. Nonetheless, from the log, I learnt that object can’t be sent as parameter. So, I modified my codes to send as string.

Client Script (before sending to server:)

Then, compiled and tested again

Emit was called without error now. But, server didn’t receive any call. I also noticed there were several onMessage calls in the Android log. So, I thought, server was sending the calls.

So, I modified server codes again to log before sending “hello”. Then I found out that server was not sending the “hello” calls. Other than replys to “rpc” calls with “rpc_ret”. So, I think, “rpc_ret” calls were being replaced by previous “hello” call. I think, they were mixed up somehow somewhere.

I don’t know how to continue as I ran out of ideas.
Perhaps, if you have working sample that you can share with, I’d be glad to have it and learn from it.

Thanks in advance again… T.T

Hope you can help me… @hannon235

@lambda@pandamicro… . Could you please help me too?

@hannon235 could you please check

@StudioAMK Is this only happening on Android, or can you test the same code on Win32 or OSX? If the issue is only on Android, are you enabling permissions for local storage and network access in your Android Manifest XML file?

This could be the cause of both warnings about storage and http classes, as well as the reason that the code works without error but no network packets are sent.

@hannon235

Edit: Just tested on Win32. Same issue. Incoming ok. Outgoing failed.

what I got form AndroidManifest.xml (looks ok?):

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>  
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>  
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>  

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Beside that, I’ve compiled and tested with Cocos2d-X JS test project yesterday. The same issue.

Could this be because of NDK / SDK / JDK versions?

Any progress on this? I’m having the same issue:

rejecting 'org/cocos2dx/lib/Cocos2dxLocalStorage'

When I comment the following code, the app works ok:

    var userData = cc.sys.localStorage.getItem("UserData");

    if (userData == null)
    {
        cc.log("No saved file... Saving new one");
        cc.sys.localStorage.setItem("UserData", JSON.stringify(LevelInfo));
    }

I’m working with Android Studio, SDK 16-22, NDK r10c, JDK 1.8, Cocos2d-js 3.7.1, and SDKBOX integrated, everything works fine if I disable the saving feature… and that’s something I can’t discard, :stuck_out_tongue:

Thanks!

@josempan have you checked AndroidManifest.xml ?

Yes. The manifest:

<uses-feature android:glEsVersion="0x00020000" />

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher">
    
    
	<meta-data android:name="android.app.lib_name" android:value="cocos2djs" />
	
    <activity android:configChanges="orientation|keyboardHidden|screenSize" android:label="@string/app_name" android:name="org.cocos2dx.javascript.AppActivity" android:screenOrientation="landscape" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


</application>


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BILLING" />