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)
1. Clone the repository
2. Open the file websockets.zip. (Inside there is a "websocket" directory)
3. Create a new cocos2djs project
6. Now, try to compile the project and see if everything works.
3. Change the file cocos2d-x-3rd-party-libs-src\contrib\src\websockets\rules.mak
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
- cd HelloWorld/frameworks/cocos2d-x/external
- mv websockets websockets_bk
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.
- Clone the repository of cocos2d-x-3rd-party-libs-src
- git clone https://github.com/cocos2d/cocos2d-x-3rd-party-libs-src
3. Change the file cocos2d-x-3rd-party-libs-src\contrib\src\websockets\rules.mak
- Look for "-DLWS_WITHOUT_SERVER=1" and remove it.
- Android
- ./build.sh --platform=android --libs=websockets
- IOS
- ./build.sh --platform=ios --libs=websockets
- You can do it for other platforms.
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
- cd HelloWorld/frameworks/cocos2d-x/external
- mv websockets websockets_bk
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.
- 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