问题描述
我尝试按照此链接在 ReactNative 中使用 SocketIOhttps://github.com/facebook/react-native/issues/4393,
在 IOS 上它运行良好,但在 Android 上它无法运行Socket 对象的结果已连接:假
I try to use SocketIO in ReactNative by follow this link https://github.com/facebook/react-native/issues/4393,
On IOS it work very well but Android it could not workResult Of Socket Object connected:false
index.android.js
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
package.json
"react-native": "0.35.0",
"socket.io-client": "^1.5.1"
我找不到类似的问题我错过了什么?
I could not found similar problemI missing something?
我不确定是否可以使用 ReactNative 在本地主机中测试 socketIO,但是当我在 IOS 模拟器上测试时它可以工作
edited : I'm not sure can I test socketIO in localhost with ReactNative but It's work when I test on IOS emulator
我的错 无法在本地环境服务器上测试但它适用于 IOS 而不是 android谁能解释一下为什么?
edited2 : My fault It cannot test on local environment server but It's work on IOS not androidCan Anybody Explained Why?
推荐答案
我也想将 Socket.IO 与 ExpressJS 服务器和 React Native 一起使用,但无法让它工作.
I also wanted to use Socket.IO with ExpressJS server and React Native but couldn't get it to work.
然后使用https://facebook.github.io/react-native/docs/network.html#websocket-support 与 https://github.com/websockets/ws
而且效果很好.
这篇关于使用 ReactNative 的套接字 IO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!