我尝试通过以下链接在 React Native 中使用 Socket IOhttps://github.com/facebook/react-native/issues/4393 https://github.com/facebook/react-native/issues/4393,
在 IOS 上运行得很好,但在 Android 上却无法运行
套接字对象的结果
连接:假
索引.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!');
});
}
// Other Code
}
package.json
:
"react-native": "0.35.0",
"socket.io-client": "^1.5.1"
我找不到类似的问题。我错过了什么吗?
编辑:
我不确定是否可以使用 ReactNative 在 localhost 中测试 socketIO,但当我在 IOS 模拟器上测试时它可以工作
编辑2:
我的错 它无法在本地环境服务器上测试
但它适用于 IOS 而非 Android
任何人都可以解释为什么吗?
我还想将 Socket.IO 与 ExpressJS 服务器和 React Native 一起使用,但无法让它工作。
然后用https://facebook.github.io/react-native/docs/network.html#websocket-support https://facebook.github.io/react-native/docs/network.html#websocket-support with https://github.com/websockets/ws https://github.com/websockets/ws
而且效果很好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)