想用在cocos2d-x 3.15上實做websocket server

我想測試在cocos2d-x 3.15上使用websocket server
由於3.15上的libwebsockets已經更新到2.10版
舊的server範例無法使用(libwebsocket_* -> lws_*)
我改用以下例子:
https://gist.github.com/zYeoman/8dffbb612d10c3a11912cb26027f4236
但結果用以下網站測試:
https://www.websocket.org/echo.html
還是無法執行
看似是沒有listen port
請問這是什麼樣的問題?
我的log如下:

[2017/08/07 16:42:40:7829] NOTICE: Initial logging level 7
[2017/08/07 16:42:40:7829] NOTICE: Libwebsockets version: 2.1.0 james@James-Chen.local-v3.4-139-gbdcf7f8
[2017/08/07 16:42:40:7829] NOTICE: IPV6 compiled in and enabled
[2017/08/07 16:42:40:7830] NOTICE: libev support not compiled in
[2017/08/07 16:42:40:7830] NOTICE: libuv support not compiled in
[2017/08/07 16:42:40:7830] NOTICE: Threads: 1 each 7168 fds
[2017/08/07 16:42:40:7831] NOTICE: mem: platform fd map: 57344 bytes
[2017/08/07 16:42:40:7832] NOTICE: Compiled with OpenSSL support
[2017/08/07 16:42:40:7832] NOTICE: SSL disabled: no LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT
[2017/08/07 16:42:40:7832] NOTICE: Creating Vhost ‘default’ port 9001, 2 protocols, IPv6 on
[2017/08/07 16:42:40:7832] NOTICE: mem: per-conn: 512 bytes + protocol rx buf
starting server…
[2017/08/07 16:42:40:8343] NOTICE: lws_protocol_init

为什么不直接下libwebsocket的库去跑server呢,库的话一定没问题,我测试过

想把server數據畫圖出來。

我也出现这个问题了 请问怎么解决呢?

還是沒有辦法,有高手可以回答嗎?

你会js绑定c++吗,如果非要这样做的话,建议你把libwebsocket server库绑定到js层,再使用(因为官方绑定的应该是client,不确定server库有绑定)

我沒有需要用到cocos2d-js,是想要製作server端,應該跟js綁定沒有關係

我是用以下範例:
仍然無法使用WebSocket
改採用cocos2d-x 3.16版
仍然有這問題
有人有解決辦法嗎?

https://github.com/iamscottmoyers/simple-libwebsockets-example/blob/master/server.c

稍微知道原因了cocos的websocket沒有編譯server的code(編譯時下了-DLWS_WITHOUT_SERVER=1)
需要重新編譯
我遵照下面指示
http://carlgira-ot.blogspot.tw/2016/11/websocketserver-cocos2djs-part-1.html
但仍然出現錯誤
錯誤如下:

/Users/joey/Downloads/cocos2d-x-3rd-party-libs-src-3/contrib/ios-armv7/websockets/lib/server.c:113:27: error:
use of undeclared identifier ‘SOL_IPV6’
if (setsockopt(sockfd, SOL_IPV6, IPV6_V6ONLY,
^
1 error generated.
make[3]: *** [CMakeFiles/websockets.dir/lib/server.c.o] Error 1
make[2]: *** [CMakeFiles/websockets.dir/all] Error 2
make[1]: *** [all] Error 2
make: *** [.websockets] Error 2

有人有解決辦法嗎?

我將rules.mak參數改為-DLWS_IPV6=0
關閉IPv6的設定
可以產出libwebsockets.h和libwebsockets.a
但仍然無法執行(同樣沒有listen的動作)
有人有方法嗎?

自己找到答案了:
需要在portocols下增加pvo

================================
static struct lws_protocols protocols[] = {
// first protocol must always be HTTP handler
{
“http-only”, // name
callback_http, // callback
0, // per_session_data_size
},
{
“default-protocol”, // protocol name - very important!
callback_example, // callback
0, // we don’t use any per session data
},
{
NULL, NULL, 0, // End of list
}
};

const struct lws_protocol_vhost_options pvo_opt = {
NULL, NULL, “default”, “1”
};

const struct lws_protocol_vhost_options pvo = {
NULL, &pvo_opt, “default-protocol”, “”
};

================================
struct lws_context_creation_info info;
memset( &info, 0, sizeof(info) );
info.pvo = &pvo; //需要增加這個項目
================================

這樣就可以連線成功了,
但有人知道怎樣使用ssl,
改成wss://服務?

自己帮自己做解答,
新版本3.18版已将Server版编译进去 https://github.com/cocos2d/cocos2d-x/issues/17944
且已经有编译好的libwebsocket库,
路径为 https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin
其实若能将Server端程式绑定为JS会更加理想(这样Cocos Creator也能使用),
不过这部分自己来也是可以,
感谢团队帮忙。

兄弟 我也遇到了 怎么操作

兄弟 请教这个怎么操作呀

我遇到过一次,是因为native上使用的是wss协议,h5使用的ws协议,但是服务器没有配置wss协议的设置。参考一下