请问我的H5项目,在Native APP中如何调用https或wss?我运行报错!

@麒麟子 大神关注下 :grinning:

#问题所在
cocos creator ver: 1.5.1 beta4
刚打算用cocos creator开发游戏,由于苹果审核原因,所以,也采用了https和wss。
目前基于PC浏览器,一切正常。测试ios和android原生打包报错。

:slightly_smiling::slightly_smiling::slightly_smiling:求官方或大神帮帮忙~:slightly_smiling::slightly_smiling::slightly_smiling:

下面是错误信息,其中的域名,我已经马赛克了。


connect to wss://www.abc.com:12345
D/WebSocket.cpp (415): WebSocket thread start, helper instance: 0x6080000b0620
D/WebSocket.cpp (167): NOTICE: Initial logging level 911
D/WebSocket.cpp (167): NOTICE: Libwebsockets version: 2.1.0 james@James-Chen.local-v3.4-139-gbdcf7f8
D/WebSocket.cpp (167): NOTICE: IPV6 compiled in and enabled
D/WebSocket.cpp (167): NOTICE: libev support not compiled in
D/WebSocket.cpp (167): NOTICE: libuv support not compiled in
D/WebSocket.cpp (167): NOTICE: Threads: 1 each 2560 fds
D/WebSocket.cpp (167): NOTICE: mem: platform fd map: 20480 bytes
D/WebSocket.cpp (167): NOTICE: Compiled with OpenSSL support
D/WebSocket.cpp (167): NOTICE: mem: per-conn: 512 bytes + protocol rx buf
D/WebSocket.cpp (873): scheme: wss, host: abc.com, port: 12345, path:
D/WebSocket.cpp (837): WARNING: CA Root file isn’t set. SSL connection will not peer server certificate
D/WebSocket.cpp (167): NOTICE: Creating Vhost ‘default’ port -1, 1 protocols, IPv6 on
D/WebSocket.cpp (167): NOTICE: lws_protocol_init
D/WebSocket.cpp (1322): WebSocket (0x7ffabf5725e0) Unhandled websocket event: 32
D/WebSocket.cpp (1322): WebSocket (0x7ffabf5725e0) Unhandled websocket event: 29
D/WebSocket.cpp (167): ERR: server’s cert didn’t look good, X509_V_ERR = 20: error:00000014:lib(0):func(0):reason(20)
D/WebSocket.cpp (1197): WebSocket (0x7ffabf5725e0) onConnectionError, state: 0 …
D/WebSocket.cpp (1226): WebSocket (0x7ffabf5725e0) onConnectionClosed, state: 2 …
D/WebSocket.cpp (1256): onConnectionClosed, WebSocket (0x7ffabf5725e0) is closing by server.


#参考提供的解决方案
参考提供了cocos-x的解决方案,需要设定CA根证书即可,但在creator里,如何去设置呢?

参考url:http://discuss.cocos2d-x.org/t/websocket-supports-ssl-connections/34611

auto ws = new WebSocket();
ws->init("wss://echo.websocket.org", nullptr, "your CA root file path");

#补充
在cocos-x中 jsb_websocket.cpp 看到有这样的映射方法:
可以在第3个参数设置根证书。
(据我初步了解jsb,应该是cocos creator原生app专用接口,我们项目即需要H5,也需要原生app)

310 bool js_cocos2dx_extension_WebSocket_constructor(JSContext *cx, uint32_t argc, jsval vp)

373 cobj = new (std::nothrow) WebSocket();
374 JSB_WebSocketDelegate
delegate = new (std::nothrow) JSB_WebSocketDelegate();
375 delegate->setJSDelegate(obj);
376 cobj->init(*delegate, url, &protocols, caFilePath);

1赞

帖子顶起来~

楼主这问题问得好,我也出了这个问题,官方文档找不到,不知道咋整

试试

var ws = new WebSocket("wss://xxx.com", protocols, cc.url.raw("caFilePath.pem"));

设置了ca证书,还是不行~换了个错误了
@dumganhar

这问题,谁指导呢?

@dumganhar 不是1.4.2就说支持了么?

头痛啊,谁能帮忙看看呢?

话说为啥一定要https呢,原生环境用http,web用https不就好了。。。

苹果要https

可以考虑用socket.io的库来做呢?

socket.io底层也是调用的ws哈

https其实和客户端无关,只要服务器上加证书就好了,长连接应该是使用域名访问就可以了,wss这套我不是很了解,但是socket直连是这样的。

服务器肯定有证书的,否则https不会正常使用的!

在Android平台可以正常使用。在ios系统中,我也遇到同样的问题,求解。我的不导入呀呀语音相关东西能正常使用,一接入就报和上面差不多的错误

certificate verify failed说明的你的证书是不合法的啊。
你的证书如何生成的?

letsencrypt申请的,使用https一切都正常,wss在pc的web下也正常,就是原生app下不正常~

你的root根证书如何生成的?
换用
http://curl.haxx.se/ca/cacert.pem
这个试试?

我试过https://www.trustasia.com/这家的,也是一样的。

你这个,我马上试试看~


刚问过,他们试过这个的,没有用。服务器需要证书和key,前端用这个也试过了。

客户端的Root证书用于校验服务端的证书是否合法。
目前看你的日志,应该是客户端的Root证书验证服务端证书的时候过不去。
你可以从firefox或者chrome导出一个可以用的Root证书再试试。