Socket.io 带本机的套接字IO

Socket.io 带本机的套接字IO,socket.io,react-native,react-native-android,Socket.io,React Native,React Native Android,我尝试通过以下链接在ReactNative中使用SocketIO , 在IOS上,它工作得很好,但安卓系统无法工作 套接字对象的结果 连接:false index.android.js window.navigator.userAgent = 'react-native';//'react-native'; const io = require('socket.io-client/socket.io'); export default class testApp exte

我尝试通过以下链接在ReactNative中使用SocketIO ,
在IOS上,它工作得很好,但安卓系统无法工作 套接字对象的结果 连接:false

index.android.js

    window.navigator.userAgent = 'react-native';//'react-native';
    const io = require('socket.io-client/socket.io');
    export default class testApp extends Component {
        componentWillMount(){
        this.socket = io.connect('http://localhost:3000', {
                      jsonp: false,
                      transports: ['websocket'] 
        });
        // Socket Object connected:false
    }
    componentDidMount(){
        console.log(this.socket)
        this.socket.on('connect', () => {
           console.log('ready to emit')
           console.log('connected!');
        });
     }
package.json

"react-native": "0.35.0",
"socket.io-client": "^1.5.1"
我没有发现类似的问题 我错过了什么

编辑: 我不确定是否可以使用ReactNative在localhost中测试socketIO,但在IOS emulator上测试时它是有效的

编辑2: 我的错,它无法在本地环境服务器上测试 但这是在IOS上的工作,而不是安卓系统
有人能解释一下原因吗?

我也想在ExpressJS服务器上使用Socket.IO和React Native,但无法让它工作

然后与

而且效果很好

这是clint中Socket.io的完整示例(我希望对您有用)

从“React”导入React;
从“socket.io客户端”导入SocketIOClient
const USER_ID='@userId';
导出默认类测试扩展React.Component{
建造师(道具){
超级(道具);
此.state={
信息:[],
userId:null
};
this.determineUser=this.determineUser.bind(this);
this.onReceivedMessage=this.onReceivedMessage.bind(this);
this.onSend=this.onSend.bind(this);
this.\u storeMessages=this.\u storeMessages.bind(this);
this.socket=SocketIOClient('http://localhost:3000');
this.socket.on('message',this.onReceivedMessage);
这是determineUser();
}
/**
*当用户加入聊天室时,检查他们是否是现有用户。
*如果没有,则向服务器请求用户ID。
*将userId设置为组件的状态。
*/
限定符(){
AsyncStorage.getItem(用户ID)
。然后((userId)=>{
//如果没有存储的用户ID,则从服务器获取一个。
如果(!userId){
this.socket.emit('userJoined',null);
this.socket.on('userJoined',(userId)=>{
AsyncStorage.setItem(用户ID、用户ID);
this.setState({userId});
});
}否则{
this.socket.emit('userJoined',userId);
this.setState({userId});
}
})
.捕获((e)=>警报(e));
}
//事件侦听器
/**
*当服务器向此服务器发送消息时。
*/
onReceivedMessage(消息){
此.\u存储消息(消息);
}
/**
*发送消息时,将消息发送到服务器
*并将其存储在此组件的状态中。
*/
onSend(消息=[]){
this.socket.emit('message',messages[0]);
此.\u存储消息(消息);
}
render(){
var user={u id:this.state.userId | |-1};
返回(
);
}
}
io.on('connection',function(client){console.log('User Joined:'))

}


这可能是由于错误造成的

import io from "socket.io-client/socket.io"
然后只需在下面一行添加

import io from "socket.io-client/dist/socket.io";
然后在componenDidMount或useEffect函数中,只需在下面一行添加它。永远不要在类组件的构造函数下使用它

 var socket = io("https://localhost.com:3000", { jsonp: false });
        // client-side
        socket.on("chat_message", (msg) => {
            console.log(msg);
        });

此外,如果纯WebSocket的级别太低。我推荐,普里默斯。与react native配合使用效果非常好。
import io from "socket.io-client/socket.io"
import io from "socket.io-client/dist/socket.io";
 var socket = io("https://localhost.com:3000", { jsonp: false });
        // client-side
        socket.on("chat_message", (msg) => {
            console.log(msg);
        });