【问题标题】:React native websocket error on Android在Android上反应本机websocket错误
【发布时间】:2015-11-16 07:42:17
【问题描述】:

我在 react 本机框架上使用简单的 Websocket 连接,在 ios 上效果很好,但在 Android 上,我 70% 收到消息:WebSocketEvent {type: "error", message: null},此错误正在关闭套接字连接。不知道为什么会这样?

我的代码是:

self._ws = new WebSocket("wss://fe01-ws.wearetv.com/platform/simchacr/some_code");

    var initPing = function (){
        pingInterval = setInterval(function(){
            ping.timestamp = new Date().getTime();
            ping.sequenceNumber++;
            ping.params.timestamp = ping.timestamp;
            ping.params.sent_ts = ping.timestamp;
            ping.params.seq = ping.sequenceNumber.toString();
            self._ws.send(JSON.stringify(ping.params));
            console.log("Ping is sent");
            isPinged = false;
            pongTimeout = setTimeout(function(){
                if (isPinged === false) {
                    self._ws.close();
                }
            }, PONG_DELAY);
        }, PING_INTERVAL);
    };


    var initConnection = (function me() {
        isPinged = false;

        self._ws.onopen = function () {
            console.log("Connection opened");
            initPing();
        };
        self._ws.onerror = function(error) {
            console.error(error);
            self._ws.close();
        };
        self._ws.onclose = function() {
            console.log("Connection closed");
            clearInterval(pingInterval);
            clearTimeout(pongTimeout);
            clearTimeout(reconnectTimeout);
            reconnectTimeout = setTimeout(function(){
                console.log("Ping reconnection");
                initConnection();
                initPing();
            }, PING_RECONNECT_TIME);
            isPinged = false;
        };
        self._ws.onmessage = function (message) {
            //var data = JSON.parse(message.data);
            //console.log(data);
            //if (data[REQUEST_PROPERTY_MAIN] === "ACK") {
            //    clearTimeout(pongTimeout);
            //    clearTimeout(reconnectTimeout);
            //    isPinged = true;
            //}
            //self._getMessage(data);
        };
        return me;
    })();

【问题讨论】:

  • 您最终找到解决方案了吗?

标签: android google-chrome websocket react-native


【解决方案1】:

您是否将 Gradle 配置为编译与从 npm 安装的版本相同的 react-native 版本?

例如:compile project(':ReactAndroid')

相对于com.facebook.react:react-native:0.12.+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-23
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多