android平台上websocket无法正常建立连接

你好,先展示我写的代码

helloworld替换onload

onLoad: function() {
        var that = this;
        cc.error('websocket init start');
        // var ws = new WebSocket("ws://echo.websocket.org");
        var ws = new WebSocket("ws://wiw-dev.leanapp.cn");
        cc.error('websocket init mid')
        ws.onopen = function(event) {
            cc.error("websocket opened.");
        };
        ws.onmessage = function(event) {
            cc.error("websocket response text msg: " + event.data);
            that.label.string = JSON.stringify(event.data);
        };
        ws.onerror = function(event) {
            cc.error("websocket Send Text fired an error");
        };
        ws.onclose = function(event) {
            cc.error("websocket instance closed.");
        };
        cc.error('websocket init end')
        that.scheduleOnce(function() {
            cc.error("websocket start to send.");
            if (ws.readyState === WebSocket.OPEN) {
                cc.error("websocket success to send.");
                ws.send("[\"hello\", {}]");
            } else {
                cc.error("websocket instance wasn't ready...");
            }
        }, 3);
        that.label.string = this.text;
    },

遇到的问题是,在web平台可以正确的建立连接,但是在android平台上无法成功建立连接。但是在android平台上连ws://echo.websocket.org是可以的。郁闷了好久,求大神给个详细的解决方案。

我查一下,这个服务器目前可以测试对吧?

你使用的creator版本是?

除了Android平台,其他native平台(mac,win32, ios)也会出现这个问题么?

我发现Mac平台没问题。
但是Android上会出现这个错误日志:

ERR: getaddrinfo: No address associated with hostname

楼上那个错误,是因为我忘记加入

<uses-permission android:name="android.permission.INTERNET"/>

到AndroidManifest.xml中了。
不知道楼主是否也犯了这个错误?
@657880970

楼主把错误人日志贴一下吧。

可以测试一直可用,用wscat 连接正常

构建出的工程默认都带这个权限

02-10 14:23:16.661 22049-22071/org.cocos2d.helloworld D/cocos2d-x debug info: websocket init start
02-10 14:23:16.663 22049-22071/org.cocos2d.helloworld D/WebSocket.cpp: [WebSocket::init] _host: wiw-dev.leanapp.cn, _port: 80, _path: /
02-10 14:23:16.663 22049-22078/org.cocos2d.helloworld D/WebSocket.cpp: WebSocket thread start, helper instance: 0xac207350
02-10 14:23:16.663 22049-22078/org.cocos2d.helloworld D/libwebsockets: NOTICE: Initial logging level 967
02-10 14:23:16.663 22049-22071/org.cocos2d.helloworld D/cocos2d-x debug info: websocket init mid
02-10 14:23:16.663 22049-22078/org.cocos2d.helloworld D/libwebsockets: NOTICE: Libwebsockets version: 1.7.8 linwenhai@LindeMacBook-Pro.local-v3.4-49-g9369eea
02-10 14:23:16.663 22049-22078/org.cocos2d.helloworld D/libwebsockets: NOTICE: IPV6 compiled in and enabled
02-10 14:23:16.663 22049-22078/org.cocos2d.helloworld D/libwebsockets: NOTICE: libev support not compiled in
02-10 14:23:16.663 22049-22071/org.cocos2d.helloworld D/cocos2d-x debug info: websocket init end
02-10 14:23:16.664 22049-22078/org.cocos2d.helloworld D/libwebsockets: NOTICE:  Threads: 1 each 1024 fds
02-10 14:23:16.664 22049-22078/org.cocos2d.helloworld D/libwebsockets: NOTICE:  mem: platform fd map:  4096 bytes
02-10 14:23:16.664 22049-22078/org.cocos2d.helloworld D/libwebsockets: NOTICE:  mem: per-conn:          292 bytes + protocol rx buf
02-10 14:23:19.701 22049-22071/org.cocos2d.helloworld D/cocos2d-x debug info: websocket start to send.
02-10 14:23:19.701 22049-22071/org.cocos2d.helloworld D/cocos2d-x debug info: websocket instance wasn't ready...
02-10 14:23:37.030 22049-22078/org.cocos2d.helloworld D/libwebsockets: NOTICE: wsi 0xac204f70: TIMEDOUT WAITING on 4 (did hdr 0, ah 0xac214cc0, wl 0, pfd events 0)
02-10 14:23:37.030 22049-22078/org.cocos2d.helloworld D/WebSocket.cpp: WebSocket (0xac1f5008) onConnectionError ...
02-10 14:23:37.031 22049-22078/org.cocos2d.helloworld D/libwebsockets: NOTICE: lws_header_table_detach: wsi 0xac204f70: ah held 21s, ah.rxpos 0, ah.rxlen 0, mode/state 13 4,wsi->more_rx_waiting 0
02-10 14:23:37.031 22049-22078/org.cocos2d.helloworld D/WebSocket.cpp: Ready state is closing or was closed, code=2, quit websocket thread!
02-10 14:23:37.031 22049-22078/org.cocos2d.helloworld D/libwebsockets: NOTICE: lws_context_destroy
02-10 14:23:37.031 22049-22078/org.cocos2d.helloworld D/WebSocket.cpp: WebSocket (0xac1f5008) onConnectionClosed ...
02-10 14:23:37.032 22049-22078/org.cocos2d.helloworld D/WebSocket.cpp: WebSocket thread exit, helper instance: 0xac207350
02-10 14:23:37.037 22049-22071/org.cocos2d.helloworld D/cocos2d-x debug info: websocket Send Text fired an error
02-10 14:23:37.037 22049-22071/org.cocos2d.helloworld D/cocos2d-x debug info: websocket instance closed.
02-10 14:23:37.037 22049-22071/org.cocos2d.helloworld D/WebSocket.cpp: In the destructor of WebSocket (0xac1f5008)

creator版本是1.3.3

我觉得是libwebsockets库的事,如果是的话,那么到哪个平台都会是有问题的,除了h5

我把apk解开看了,确实有android.permission.INTERNET这个权限

看到了这个日志,目测是ws链接你的服务器超时了。
你测试下其他平台看看?如果都会,估计是你的服务器配置哪里有问题吧?

不是,web就可以

win32或者mac呢?

我这里测试你的服务器,是可以连接上的。

检查下服务器返回的头信息,记得我最初自己写websocket服务器时,在测试时有的平台可以handshake,有的平台不行,参照libwebsocket服务端返回的handshake信息后,才了解,同样的handshake返回的信息可能在浏览器上行,但在native上不行,最后我保留了以下handshake返回信息,测试h5和native都能连,如下:
answer += “HTTP/1.1 101 Switching Protocols\r\n”;
answer += “Upgrade: WebSocket\r\n”;
answer += “Connection: Upgrade\r\n”;
//answer += “Sec-WebSocket-Version: 13\r\n”;
answer += “Sec-WebSocket-Protocol:” + protocol + “\r\n”;
answer += ("Sec-WebSocket-Accept: " + key + “\r\n”);
其中,anwser就是handshake返回的头信息,注释的那部分就是我经过多次测试,去掉的部分,才能同时连h5和native

我当时测试时好像是有的浏览器行,有的浏览器不行,然后就像上面那样改了,如果楼主是服务端用的第三方的websocket库,应该就不用担心,连不上的问题

我用的就是nodejs + ws库,本地部署能连接,部署到云引擎上后响应的头部大小写都变了,我觉得这是连不上的原因

HTTP/1.1 101 Switching Protocols
Server: Tengine
Date: Thu, 05 Jan 2017 03:06:08 GMT
Connection: upgrade
upgrade: websocket
sec-websocket-accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
sec-websocket-protocol: default-protocol