如何在React-native中连接到套接字?

时间:2019-02-21 14:07:14

标签: react-native

我尝试使用socket.io-client连接到套接字,但是我只得到“ SocketRocket:处于调试模式。允许连接到任何根证书”。我检查了库的不同版本并进行了本机反应,但没有任何反应。我认为该服务器的地址不正确,但经过检查,它可以正常工作。我还在本地主机上创建了节点js,将其连接到该主机,一切正常,但未连接到远程服务器。 “ socket.io-client”:“ 2.0.4”,“ react-native”:“ 0.58.3”。

import SocketIOClient from 'socket.io-client'

componentDidMount(){

this.socket = SocketIOClient('wss://bitshares.openledger.info/ws',{
  'force new connection': true,
    reconnection: true,
    reconnectionDelay: 10000,
    reconnectionDelayMax: 60000,
    reconnectionAttempts: 'Infinity',
    timeout: 10000,
    transports: ['websocket']
});
this.socket.connect();
// this.socket.send('123');
this.socket.on('connect', () => {
    console.log("CONNECTED")
});
this.socket.onopen=function(){
    console.log("onopen")
}

}

1 个答案:

答案 0 :(得分:0)

var ws = new WebSocket('wss://bitshares.openledger.info/ws');

    ws.onopen = () => {
        console.log("onopen");
        // connection opened
    };

    ws.onmessage = (e) => {
        // a message was received
        console.log("onmessage",e.data);
    };

    ws.onerror = (e) => {
        // an error occurred
        console.log(e.message);
    };

    ws.onclose = (e) => {
        // connection closed
        console.log(e.code, e.reason);
    };