Cocos 3.4.0打包安卓native,出现无法访问wss连接的问题。

  • Creator 版本: 3.4.0

  • 目标平台: Android真机

  • 重现方式:JS代码中创建wss连接,打包成安卓原生

  • 首个报错:

2022-04-22 22:07:52.232 17346-17520/com.xxx.yyy D/WebSocket.cpp: WebSocket thread start, helper instance: 0x7c7692d840

2022-04-22 22:07:52.239 17346-17520/com.xxx.yyy D/WebSocket.cpp: scheme: wss, host: xxx.yyy.com, port: 0, path: /04221/webSocket?userId=ea38620be402a44e0da9bd765

2022-04-22 22:07:52.239 17346-17520/com.xxx.yyy D/WebSocket.cpp: WARNING: CA Root file isn’t set. SSL connection will not peer server certificate

2022-04-22 22:07:52.239 17346-17520/com.xxx.yyy D/WebSocket.cpp: NOTICE: Creating Vhost ‘default’ port -1, 1 protocols, IPv6 on

2022-04-22 22:07:52.240 17346-17520/com.xxx.yyy D/WebSocket.cpp: NOTICE: created client ssl context for default

2022-04-22 22:07:52.240 17346-17520/com.xxx.yyy D/WebSocket.cpp: NOTICE: lws_client_connect_2: 0x7cded14400: address xxx.yyy.com

2022-04-22 22:07:52.263 17346-17520/com.xxx.yyy D/WebSocket.cpp: NOTICE: uplevelling AF_INET to AF_INET6

2022-04-22 22:07:52.263 17346-17520/com.xxx.yyy D/WebSocket.cpp: WebSocket (0x7ceb6e6a80) Unhandled websocket event: 32

2022-04-22 22:07:52.263 17346-17520/com.xxx.yyy D/WebSocket.cpp: WebSocket (0x7ceb6e6a80) Unhandled websocket event: 29

2022-04-22 22:07:52.292 17346-17520/com.xxx.yyy D/WebSocket.cpp: NOTICE: lws_client_connect_2: 0x7cded14400: address xxx.yyy.com

2022-04-22 22:07:52.297 17346-17520/com.xxx.yyy D/WebSocket.cpp: NOTICE: uplevelling AF_INET to AF_INET6

2022-04-22 22:07:52.330 17346-17520/com.xxx.yyy D/WebSocket.cpp: WebSocket (0x7ceb6e6a80) Unhandled websocket event: 58

2022-04-22 22:07:52.330 17346-17520/com.xxx.yyy D/WebSocket.cpp: ERR: SSL error: unable to get local issuer certificate (preverify_ok=0;err=20;depth=1)

2022-04-22 22:07:52.330 17346-17520/com.xxx.yyy D/WebSocket.cpp: ERR: SSL connect error 337047686: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed

2022-04-22 22:07:52.330 17346-17520/com.xxx.yyy D/WebSocket.cpp: WebSocket (0x7ceb6e6a80) onConnectionError, state: 0 …

2022-04-22 22:07:52.330 17346-17520/com.xxx.yyy D/WebSocket.cpp: WebSocket (0x7ceb6e6a80) onConnectionClosed, state: 2 …

2022-04-22 22:07:52.330 17346-17520/com.xxx.yyy D/WebSocket.cpp: onConnectionClosed, WebSocket (0x7ceb6e6a80) is closing by server.

2022-04-22 22:07:52.330 17346-17520/com.xxx.yyy D/WebSocket.cpp: WebSocket (0x7ceb6e6a80) onConnectionClosed DONE!

  • 之前哪个版本是正常的: 无

  • 手机型号: redmi note9

  • 编辑器操作系统: windows

  • 重现概率: 必现

  • 尝试过的解决方案:

  • 安卓的AndroidManifest中设置android:networkSecurityConfig,并配置pem文件(官方示例项目和自己尝试导出的根证书均无效)
  • 使用不安全的ws连接(可行,但不符合初衷)
  • 2.x的方案,在new Websocket中传入第三个参数,3.x中不可行,js代码运行报错

后面如何解决的?

解决了吗?