I am learning React Native recently and have problems using Socket.IO. I am using the latest version of React native and cli (just updated) and this is my code:
import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View } from 'react-native'; window.navigator.userAgent = 'react-native'; const io = require('socket.io-client/socket.io'); const socket = io('localhost:3000', { jsonp: false }); socket.connect(); class wschat extends Component { ... }
You can see the code is pretty simple and there is no error. This is my server code:
"use strict"; const express = require('express'); const app = express(); const http = require('http').Server(app); const io = require('socket.io')(http); http.listen(3000, () => { console.log('WSChat Server for React-Native listening on port 3000'); }); io.on('connection', (socket) => { console.log('connected: ' + socket.id); });
Seems to be beautiful, and it actually works with ios, but does not work on android. I turned on the remote debugger and checked the properties, and Socket.IO itself it was loaded well, but the connection was not established. You can see the server code when the "connection" event occurred, registered on the console.
I used AVD (nexus5) and my device (LG G4 optimus), but both will not work. What am I missing? Any advice would be greatly appreciated for that!
source share