viernes, 18 de noviembre de 2016

WebSocketServer Cocos2djs - part 1 - libwebsocket

This is the first 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 first entry is going to be about how to create the libraries needed to use the WebSocketServer library.

Libwebsockets is a C library included inside Cocos2d-x to handle websockets. The thing, is that by default the functionality of the "server" is disabled in Cocos2d-x, so its necessary to re-compile the library changing one flag.

The easiest way is to download from GitHub the  precompiled zip so you can  use it directly, or maybe if you want, I'm leaving instructions so you can compile it yourself.

(tested with cocos2djs 3.13.1)

PRECOMPILED LIBWEBSOCKET

 I'm including a prebuilt libwebsocket in the git repository so you can add the lib directly to your project.

1. Clone the repository
 2. Open the file websockets.zip. (Inside there is a "websocket" directory)
 3. Create a new cocos2djs project
  • cocos new HelloWorld -l js
4. Create a backup of the original libraries.
  • cd HelloWorld/frameworks/cocos2d-x/external
  • mv websockets websockets_bk
 5. Copy the new "websocket" dir inside the "external" directory.

6. Now, try to compile the project and see if everything works.
  • cocos compile -p android

RECOMPILE LIBWEBSOCKETS

Enabling WebSocketServer in Libwebsockets

The "server" function of the libwebsocket library is disabled by default so it's necessary, to recompile the library changing one flag.  
  1. Clone the repository of cocos2d-x-3rd-party-libs-src
    • git clone https://github.com/cocos2d/cocos2d-x-3rd-party-libs-src
      2. Follow the instructions on the github page (you are going to need to install, cmake, autoconf, automake and libtool)

      3. Change the file cocos2d-x-3rd-party-libs-src\contrib\src\websockets\rules.mak
    • Look for "-DLWS_WITHOUT_SERVER=1" and remove it.
      4. Now it's time to recompile. Build the library for the number of platforms you want. I did it and test it for android and ios.  Go to the build directory and execute the build script.
  • Android
    • ./build.sh --platform=android --libs=websockets
  • IOS
    •  ./build.sh --platform=ios --libs=websockets
  • You can do it for other platforms.
       5. If everything went well, you'll  have an "android" and a "ios" directory with the compiled libraries and the .h files.

Include the library to your project

 You can add the library to your cocos2d-x installation but is better if you test it first in a new project.

1. Create a new cocos2djs project
  • cocos new HelloWorld -l js
2. Create a backup of the original libraries.
  • cd HelloWorld/frameworks/cocos2d-x/external
  • mv websockets websockets_bk
3. Now it's time to copy the library. The thing is that the directory structure from the cocos2d-x-3rd-party-libs-src and the one in the project is TOTALLY different, so you'll have to do some magic to have the same directory tree structure and copy the files from one place to the other.





 4. After finish the previous step, you'll have to copy two more files.
  • Look for a file called lws_config.h inside the cocos2d-x-3rd-party-libs-src\contrib directory and copy the file to the "include" directory of the websocket dir for each platform. 
    • The recompilation has worked, if inside this file there is commented  variable called LWS_NO_SERVER.
    • You are going to find several lws_config.h files. Choose one file from an android directory and other from an ios directory.


  • Go to the backup websocket directory and open the android folder. Copy the file Android.mk in the same place in your new websocket dir.
5. Now, try to compile the project and see if everything works.
  • cocos compile -p android

This was the first step. The next entries are going to explain some details on the c++ implementation, the javascript binding, the configuration in android and ios projects, and the explanation of the javascript api.

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