viernes, 18 de noviembre de 2016

WebSocketServer Cocos2djs - part 4 - ios

This is the fourth 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 fourth entry is going to be about on how to create a simple sample of a WebSocket server for cocos2djs using ios app.

(tested with cocos2djs 3.13.1)

Cocos2djs WebSocket Server in IOS

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

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

4. Inside the "Classes" directory modify the file "AppDelegate.cpp".
  • Add at the beginning the include to our new javascript binding .h.
    • #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);
6. Add the new four files to the Xcode Project.
7. Add also the libwebscoket.h and lws_config.h from the cocos2x-x\external\websockets\include\ios. (Your Clases directory must look like this)


7. Compile the project

Running the application

The app has three buttons and a console where it prints all the events. You can:
  • Stop a server
  • Broadcast message
  • 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 Iphone 5c.



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