简体   繁体   中英

cannot connect to socket.io server from React Native app when using expo on iPhone

for some reason I can connect to the server from the browser, but not from an iPhone. Anyone know why? I have a feeling it has something to do with SSL certs but I can't find a solution online that works.

server:

from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, cors_allowed_origins="*")

@socketio.on('connect')
def handle_json(json):
    print('CONNECTED')

if __name__ == '__main__':
    socketio.run(app)

client:

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { Text, View } from 'react-native';
import { io } from "socket.io-client";

var socket = io("http://127.0.0.1:5000/")

class App extends React.Component {
  render() {
    return (
      <View>
        <Text> test </Text>
        <StatusBar style="auto" />
      </View>
    );
  }
  
}

export default App

Is server and iPhone in same network? If not maybe ur server's firewall rules does not allows to request. iOS on default does not allows http(not secure) requests. You must change ur info.plist file u must add NSAppTransportSecurity key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM