WebSocket 在浏览器运行正常,在模拟器和真机上出错!!!

代码是官方文档给的例子:


下面是日志:
Simulator: listen:
Simulator: debugger: none
Simulator: add searching path:
Simulator:
3
Simulator: ------------------------------------------------
Simulator: LOAD Js FILE: main.js
Simulator: ------------------------------------------------
Simulator: Cocos2d-JS v3.14
Simulator: iShow!
Simulator: 加载事件!
Simulator: [WebSocket::init] _host: 172.17.134.27, _port: 9092, _path: /
Simulator: WebSocket thread start, helper instance: 0877C678
Simulator: NOTICE: Initial logging level 967
Simulator:
Simulator: NOTICE: Libwebsockets version: 1.6.0 unknown-build-hash
Simulator:
Simulator: NOTICE: IPV6 not compiled in
Simulator:
Simulator: NOTICE: libev support not compiled in
Simulator:
Simulator: WebSocket (087C7490) onConnectionError …
Simulator: NOTICE: mem: per-conn: 280 bytes + protocol rx buf
Simulator:
Simulator: Ready state is closing or was closed, code=2, quit websocket thread!
Simulator: NOTICE: lws_context_destroy
Simulator:
Simulator: WebSocket (087C7730) onConnectionClosed …
Simulator: WebSocket thread exit, helper instance: 0877C678
Simulator: 错误error
Simulator: 连接已经关闭
Simulator: In the destructor of WebSocket (087C7730)
Simulator: jsb: ERROR: File …\manual\network\jsb_websocket.cpp: Line: 397, Function: js_cocos2dx_extension_WebSocket_get_readyState
Simulator: Invalid Native Object
Simulator: 30:Error: Invalid Native Object
Simulator:
Simulator: jsb: ERROR: File …\manual\network\jsb_websocket.cpp: Line: 397, Function: js_cocos2dx_extension_WebSocket_get_readyState
Simulator: Invalid Native Object
Simulator: 30:Error: Invalid Native Object

这问题貌似不止我一个人遇到,网上也找不到答案,群里也没人回复,焦灼ing…

图片好像不高清,贴上代码:

var ws;
cc.Class({
extends: cc.Component,
properties:{
myPro:0
},
onLoad:function(){
cc.log(“加载事件!”);
ws = new WebSocket(“ws://172.17.134.27:9092”);
ws.onopen = function (event) {
console.log(“已连接”);
ws.send(“连接成功!”);
};
ws.onmessage = function (event) {
console.log(“收到消息:” + event.data);
};
ws.onerror = function (event) {
console.log(“错误” + event.type);
};
ws.onclose = function (event) {
console.log(“连接已经关闭”);
};

    setTimeout(function () {
        if (ws.readyState === WebSocket.OPEN) {
            ws.send("Hello WebSocket, I'm a text message.");
        }
        else {
            console.log("websocket未准备好");
        }
    }, 3);
}

});

这个Game.js是绑定在 canvas上的

@panda

你遇到的问题是 WebSocket 并没有连接成功,在关闭连接时 WebSocket 的原生对象被释放了,导致 setTimeout 中的代码访问 Native 对象出错。可以在 onclose 中设置一个私有状态,在 setTimeout 中检查这个状态来判断 WebSocket 是否还可以访问,如果不行就不要执行这样的代码。其次,最好检查一下为什么 websocket 连接失败

谢谢,我确定浏览器和安卓真机每次都能正常连接并发送和接受消息。但模拟器就是不行

遇到同样的问题 浏览器正常 真机器正常 google官方模拟器不行。open 后直接崩溃。
海马模拟器 直接是打不开应用。

我依然没解决这个问题,顶一下

楼主现在解决了吗?

依然没有解决,看起来是服务器拒绝了这个连接,不过我并不知道是怎么回事以及如何去做

解决了。。。,用1.5就行了。。

我是 1.4 ok 1.5不行

我的也是1.4ok 1.5 不行,你的问题解决了吗?求解!

Cocos Creator 1.9.0 最新版

模拟器关闭websocket时报错:
Simulator: E/jswrapper (447): [ERROR] (…\manual\jsb_websocket.cpp, 447): wrong number of arguments: 2, was expecting 0
Simulator: E/jswrapper (450): [ERROR] Failed to invoke WebSocket_close, location: …\manual\jsb_websocket.cpp:450

浏览器正常。

难懂啊就没人来结个帖子么,真的是难受

同样的代码,web是可以连接成功的,证书也是加了的