2015-11-16 90 views
0

我在反應原生框架上使用簡單的Websocket連接,它的工作原理非常好,但在Android上,我在70%得到消息:WebSocketEvent {type: "error", message: null},並且此錯誤正在關閉套接字連接。我不知道爲什麼它會發生?在Android上反應本機websocket錯誤

我的代碼是:

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; 
    })(); 
+0

你最終找到一個解決的辦法? – Erik

回答

2

你有沒有配置搖籃編譯相同的反應,原生版本,你從故宮安裝了一個?

例如:compile project(':ReactAndroid')

,而不是com.facebook.react:react-native:0.12.+