Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support websocket server #1921

Merged
merged 23 commits into from Nov 27, 2019

Conversation

PatriceJiang
Copy link
Contributor

@PatriceJiang PatriceJiang commented Nov 4, 2019

@PatriceJiang
Copy link
Contributor Author

@PatriceJiang PatriceJiang commented Nov 4, 2019

quick demo

Enable WebSocket server bindings

Edit ccConfig.hpp set USE_WEBSOCKET_SERVER to 1

OR

Add definition in project files(VS, Xcode, Android.mk etc.) .

Edit JS code

if(typeof WebSocketServer == "undefined") {
    console.error("WebSocketServer is not enabled!");
    return;
}


let s = new WebSocketServer();
s.onconnection = function(conn) {
    conn.ondata = function(data) {
        conn.send(data, (err)=>{});
    }
    conn.onclose = function() { console.log("connection gone!");} ;
};

s.onclose = function() {
  console.log("server is closed!")
}
s.listen(8080, (err) => {
   if(!err) console.log("server booted!");
});

API

Server

Methods

server.listen(port, [host], [callback])

server.close([callback])

Properties

connections

onconnection

callback triggered when new connection incoming.

onclose

Client

Methods

close([code], [reason])

send(binary|string, [callback]

Properties

ontext

onbinary

onconnect

onclose

ondata

headers

readyState

@PatriceJiang
Copy link
Contributor Author

@PatriceJiang PatriceJiang commented Nov 4, 2019

@minggo 麻烦有空review 下

@PatriceJiang PatriceJiang marked this pull request as ready for review Nov 5, 2019
cocos/network/WebSocketServer.h Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.h Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.h Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.h Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.h Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.h Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.cpp Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.cpp Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.cpp Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.cpp Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.cpp Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.cpp Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.cpp Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.h Outdated Show resolved Hide resolved
cocos/network/WebSocketServer.cpp Outdated Show resolved Hide resolved
@PatriceJiang
Copy link
Contributor Author

@PatriceJiang PatriceJiang commented Nov 18, 2019

已处理

@brantxiong
Copy link

@brantxiong brantxiong commented Nov 20, 2019

Hi, @PatriceJiang seems compile failed.

Enviroment

Steps

  1. compile cocos2d-x-lite engine: Success
==> Copying files...
==> Cleaning...
[22:26:42] Finished 'init' after 1.55 min
  1. Build Engine in Cocos Creator : Success
  2. Compile Engine in Cocos Creator: Failed
[armeabi-v7a] Compile++ arm  : cocos2dx_static <= jsb_socketio.cpp
  In file included from E:/github/Learn/ws/cocos2d-x-lite/cocos/network/WebSocketServer.cpp:33:
  E:/github/Learn/ws/cocos2d-x-lite/cocos/network/WebSocketServer.h:210:52: error: copying member subobject of type 'std::atomic<ReadyState>' invokes deleted constructor
              std::atomic<ReadyState>  _readyState = ReadyState::CLOSED;
                                                     ^~~~~~~~~~~~~~~~~~
  D:/Android/ndk-bundle/build//../sources/cxx-stl/llvm-libc++/include\atomic:1085:7: note: copy constructor of 'atomic<cocos2d::network::WebSocketServerConnection::ReadyState>' is implicitly deleted because base class '__atomic_base<cocos2d::network::WebSocketServerConnection::ReadyState>' has a deleted copy constructor
      : public __atomic_base<_Tp>
        ^
  D:/Android/ndk-bundle/build//../sources/cxx-stl/llvm-libc++/include\atomic:984:5: note: '__atomic_base' has been explicitly marked deleted here
      __atomic_base(const __atomic_base&) = delete;
      ^
  In file included from E:/github/Learn/ws/cocos2d-x-lite/cocos/network/WebSocketServer.cpp:33:
  E:/github/Learn/ws/cocos2d-x-lite/cocos/network/WebSocketServer.h:309:59: error: copying member subobject of type 'std::atomic<ServerThreadState>' invokes deleted constructor
              std::atomic<ServerThreadState> _serverState = ServerThreadState::NOT_BOOTED;
                                                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  D:/Android/ndk-bundle/build//../sources/cxx-stl/llvm-libc++/include\atomic:1085:7: note: copy constructor of 'atomic<cocos2d::network::WebSocketServer::ServerThreadState>' is implicitly deleted because base class '__atomic_base<cocos2d::network::WebSocketServer::ServerThreadState>' has a deleted copy constructor
      : public __atomic_base<_Tp>
        ^
  D:/Android/ndk-bundle/build//../sources/cxx-stl/llvm-libc++/include\atomic:984:5: note: '__atomic_base' has been explicitly marked deleted here
      __atomic_base(const __atomic_base&) = delete;
      ^
  E:/github/Learn/ws/cocos2d-x-lite/cocos/network/WebSocketServer.cpp:42:25: error: copying variable of type 'std::atomic_int32_t' (aka 'atomic<int>') invokes deleted constructor
      std::atomic_int32_t _aliveServer = 0; //debug info
                          ^              ~
  D:/Android/ndk-bundle/build//../sources/cxx-stl/llvm-libc++/include\atomic:1085:7: note: copy constructor of 'atomic<int>' is implicitly deleted because base class '__atomic_base<int>' has a deleted copy constructor
      : public __atomic_base<_Tp>
        ^
  D:/Android/ndk-bundle/build//../sources/cxx-stl/llvm-libc++/include\atomic:1004:7: note: copy constructor of '__atomic_base<int, true>' is implicitly deleted because base class '__atomic_base<int, false>' has a deleted copy constructor
      : public __atomic_base<_Tp, false>
        ^
  D:/Android/ndk-bundle/build//../sources/cxx-stl/llvm-libc++/include\atomic:984:5: note: '__atomic_base' has been explicitly marked deleted here
      __atomic_base(const __atomic_base&) = delete;
      ^
  [armeabi-v7a] Compile++ arm  : cocos2dx_static <= jsb_websocket.cpp
  [armeabi-v7a] Compile++ arm  : cocos2dx_static <= jsb_websocket_server.cpp
  3 errors generated.
  make: *** [E:/github/Learn/tutorial-blackjack-2019/build/jsb-link/frameworks/runtime-src/proj.android-studio/app/build/intermediates/ndkBuild/release/obj/local/armeabi-v7a/objs/cocos2dx_static/network/WebSocketServer.o] Error 1

Could you please help to check?

@brantxiong
Copy link

@brantxiong brantxiong commented Nov 20, 2019

Thanks for quick fix! After update, we can compile success, and run app on android device. But Game crashes on startup:

2019-11-20 11:42:34.165 10167-10198/? E/jswrapper: ScriptEngine::evalString script jsb-adapter/jsb-engine.js, failed!
2019-11-20 11:42:34.165 10167-10198/? E/jswrapper: [ERROR] Failed to invoke require, location: E:/github/Learn/ws/cocos2d-x-lite/cocos/scripting/js-bindings/manual/jsb_global.cpp:292
2019-11-20 11:42:34.166 10167-10198/? E/jswrapper: ERROR: Uncaught TypeError: Cannot read property 'getInstance' of undefined, location: jsb-adapter/jsb-engine.js:0:0
    STACK:
    [0]anonymous@jsb-adapter/jsb-engine.js:1346
    [1]6@jsb-adapter/jsb-engine.js:1353
    [2]o@jsb-adapter/jsb-engine.js:1
    [3]anonymous@jsb-adapter/jsb-engine.js:1
    [4]1../jsb-assets-manager.js@jsb-adapter/jsb-engine.js:39
    [5]o@jsb-adapter/jsb-engine.js:1
    [6]r@jsb-adapter/jsb-engine.js:1
    [7]anonymous@jsb-adapter/jsb-engine.js:1
    [8]anonymous@main.js:217
2019-11-20 11:42:34.166 10167-10198/? E/jswrapper: ScriptEngine::evalString script main.js, failed!

@PatriceJiang
Copy link
Contributor Author

@PatriceJiang PatriceJiang commented Nov 20, 2019

@brantxiong 这个PR 已经 rebase到v2.2.1, 你切换到cocos creator 2.2试一下

@PatriceJiang
Copy link
Contributor Author

@PatriceJiang PatriceJiang commented Nov 26, 2019

@minggo 麻烦合并下

@minggo minggo merged commit f222149 into cocos:v2.2.1-release Nov 27, 2019
1 check passed
@brantxiong
Copy link

@brantxiong brantxiong commented Nov 27, 2019

Thanks, cocos creator v 2.1.2 works Now! Following versions are checked:

1. Cocos Creator V2.1.2

2. Cocos Creator V2.2.0

2019-11-26 15:55:00.509 25311-25341/? E/jswrapper: [ERROR] (E:/github/Learn/ws/cocos2d-x-lite/cocos/scripting/js-bindings/auto/jsb_renderer_auto.cpp, 3594): wrong number of arguments: 11, was expecting 12
2019-11-26 15:55:00.509 25311-25341/? E/jswrapper: [ERROR] Failed to invoke js_renderer_NodeMemPool_updateNodeData, location: E:/github/Learn/ws/cocos2d-x-lite/cocos/scripting/js-bindings/auto/jsb_renderer_auto.cpp:3597
    
    
    --------- beginning of crash
2019-11-26 15:55:00.510 25311-25341/? A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 25341 (GLThread 595)

Waiting for your Release!

Looking forward to the release of this feature! Thanks all for the support!

@PatriceJiang PatriceJiang deleted the v2.1.4-support-websocket branch Jun 15, 2020
PatriceJiang added a commit to PatriceJiang/cocos2d-x-lite that referenced this issue Sep 28, 2020
minggo pushed a commit that referenced this issue Sep 29, 2020
* support websocket server  (#1921)

* add websocket server

* disable websocket server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants