WebSocket 直接断开问题

直接新建 一个 Hello Word 工程
// var GSocket = null;

cc.Class({
extends: cc.Component,

properties: {
    label: {
        default: null,
        type: cc.Label
    },
    // defaults, set visually when attaching this script to the Canvas
    text: 'Hello, World!'
},



// use this for initialization
onLoad: function () {
    this.label.string = this.text;

    // GSocket = new WebSocket("ws://192.168.1.131:8400");
    this.GSocket = new WebSocket("ws://192.168.1.131:8400");

    // GSocket.onopen = function (event) {
    this.GSocket.onopen = function (event) {
        console.log("onopen");
    };

    // GSocket.onerror = function (event) {
    this.GSocket.onerror = function (event) {
        console.log("-------------onerror");
    };

    // GSocket.onclose = function (event) {
    this.GSocket.onclose = function (event) {
        console.log("---------------onclose");
    };

    // GSocket.onmessage = function (event) {
    this.GSocket.onmessage = function (event) {
        console.log("onmessage");
    };

},

// called every frame
update: function (dt) {

},

});

浏览器运行 没问题 模拟器运行 出错 Log如下
2016-12-22T12:44:42.492Z - normal: Simulator: Cocos2d-JS v3.13

2016-12-22T12:44:42.528Z - normal: Simulator: iShow!

2016-12-22T12:44:42.609Z - normal: Simulator: [WebSocket::init] _host: 192.168.1.131, _port: 8400, _path: /

2016-12-22T12:44:42.609Z - normal: Simulator: WebSocket thread start, helper instance: 0974AA80

2016-12-22T12:44:42.609Z - normal: Simulator: NOTICE: Initial logging level 967

2016-12-22T12:44:42.609Z - normal: Simulator:

2016-12-22T12:44:42.610Z - normal: Simulator: NOTICE: Libwebsockets version: 1.6.0 unknown-build-hash

2016-12-22T12:44:42.610Z - normal: Simulator:

2016-12-22T12:44:42.610Z - normal: Simulator: NOTICE: IPV6 not compiled in

2016-12-22T12:44:42.610Z - normal: Simulator:

2016-12-22T12:44:42.610Z - normal: Simulator: NOTICE: libev support not compiled in

2016-12-22T12:44:42.610Z - normal: Simulator:

2016-12-22T12:44:42.610Z - normal: Simulator: NOTICE: mem: per-conn: 280 bytes + protocol rx buf

2016-12-22T12:44:42.610Z - normal: Simulator:

2016-12-22T12:44:42.611Z - normal: Simulator: WebSocket (09283390) onConnectionError …

2016-12-22T12:44:42.611Z - normal: Simulator: Ready state is closing or was closed, code=2, quit websocket thread!

2016-12-22T12:44:42.611Z - normal: Simulator: NOTICE: lws_context_destroy

2016-12-22T12:44:42.611Z - normal: Simulator:

2016-12-22T12:44:42.611Z - normal: Simulator: WebSocket (09283390) onConnectionClosed …

2016-12-22T12:44:42.611Z - normal: Simulator: WebSocket thread exit, helper instance: 0974AA80

2016-12-22T12:44:42.611Z - normal: Simulator: -------------onerror

2016-12-22T12:44:42.611Z - normal: Simulator: ---------------onclose

2016-12-22T12:44:45.173Z - normal: Simulator: In the destructor of WebSocket (09283390)

2016-12-22T12:44:45.174Z - normal: Simulator: basic_object_finalize 07D315C0 …

2016-12-22T12:44:45.175Z - normal: Simulator: jsbindings: finalizing JS object 07D315E0 (WebSocket)

2016-12-22T12:44:45.175Z - normal: Simulator: jsbindings: finalizing JS object 07D31600 (SocketIO)

2016-12-22T12:44:45.175Z - normal: Simulator: jsbindings: finalizing JS object 07D31A20 (WebSocket)

1赞

我也有相同问题,报错信息如下:
Simulator: [WebSocket::init] _host: api.pipgame.com, _port: 80, _path: /cable
Simulator: WebSocket thread start, helper instance: 0AD08390
Simulator: NOTICE: Initial logging level 967
Simulator:
Simulator: NOTICE: Libwebsockets version: 1.6.0 unknown-build-hash

1赞

你好,这个问题你解决了吗? 我也遇到这个问题

@javy_liu, @lyscb
你们用什么语言搭建的服务器?

早期的版本,默认的subprotocol 名称为: ‘default-protocol’,
你们确认下服务器是否在select protocol的时候放行这个protocol。

c与lua的服务器skynet,用的第三方的luawebsocket.https://github.com/Skycrab/skynet_websocket

以前用cocos-lua一直很好用,现在想用ceator,发现web没问题,原生出现这个问题 正在排查协议

服务器protocol都是放行的,现在修改成protocol都可以接收,依然是没解决

已经解决,是’default-protocol’,被屏蔽掉了

OK, 下个版本会彻底修复这个问题。并且支持wss模式。

请问楼主是怎么解决的?我也遇到了相同的问题,你的意思是不是说在服务端对default-protocol放行?

同问,我也遇到了相同问题,四处求解

是的 服务器你可以用log输出定位下,是哪里没有放行default-protocol,应为web和原生传过去的协议是不一样的,web传过去是null,原生传过去default-protocol

嗯,好的,谢谢了~顺便想问下,这种问题客户端有办法控制吗?就是说在客户端是否能够解决?

如果服务器端用的是sfs,应该是没有default-protocol的吧

pomelo 怎么修改这个 知道么?

貌似现在2.0.10 也有这个问题