viernes, 18 de noviembre de 2016

WebSocketServer Cocos2djs - part 3 - android

This is the third blog entry that I'm going to write to explain what was the procedure i follow to create a native WebSocket Server for Cocos2dJs and examples to test it in android and ios.

This third entry is going to be about on how to create a simple sample of a WebSocket server for cocos2djs using android.

(tested with cocos2djs 3.13.1)

Cocos2djs WebSocket Server in Android


1. Create a new Cocos2djs project
  • cocos new HelloProject -l js 
 2. Follow the instructions in the first blog entry and configure the libwebsocket library.

3. From the wsserver-cocos2djs\src copy the four c++ files to the directory HelloWorld/frameworks/runtime-src/Classes

4. Inside the "Classes" directory modify the file "AppDelegate.cpp".
  • Add at the beginning the include to our new javascript binding
    • #include "jsb_websocketserver.h"
  • In the middle of the file inside the function AppDelegate::applicationDidFinishLaunching(), register the new websocket server javascript binding functions
    •  sc->addRegisterCallback(register_jsb_websocketserver);
5. From the wsserver-cocos2djs copy the file Android.mk to the directory HelloWorld/frameworks/runtime-src/proj.android/jni (do a backup of the existing Android.mk in that directory)

6. Finally, copy the wsserver-cocos2djs\app.js to the HelloProject\src\

7. Compile the project.and make sure everything works.

Running the application

The app has three buttons and a console where it prints all the events. You can:
  • Stop a server
  • Send a broadcast message to all clients
  • Create new client connection.
To test it, you need to connect your phone to a WIFI. (it needs an IP address)

You can also test the server using the wsserver-cocos2djs\test-server.html. Modify the file and put the IP address of your phone.



Tested on a Nexus 5.


Part 1 - WebSocketServer Cocos2djs - libwebsocket
Part 2 - WebSocketServer Cocos2djs - c++ and javascript binding
Part 3 - WebSocketServer Cocos2djs - android
Part 4 - WebSocketServer Cocos2djs - ios




No hay comentarios:

Publicar un comentario