将 React Native Socket.io-客户端与 Python 服务器连接
Connecting React Native Socket.io-client with Python Server
我正在尝试将 React Native 客户端连接到开放的 Python 服务器
问题是我阅读了几个关于如何与 socket.io-客户端 API 建立连接的解决方案,但没有任何解决办法。
就是连接不上。服务器端和客户端我都会post,改了好几次
编辑*1:服务器端工作,我使用 socket.io 和 python 套接字对其进行了测试。但是作为 React 的客户端不工作
import React,{useState, useEffect} from 'react';
import { StyleSheet, Text, View, Pressable, Button } from 'react-native';
import socketIO from "socket.io-client";
export default class App extends React.Component {
constructor(props){
super(props)
this.state = {
string:"null",
chatMessage:"null",
chatMessages:[]
}
}
componentDidMount() {
this.socket = socketIO('192.168.0.107:15156')
this.socket.connect();
this.socket.on('connect', () => {
console.log('connected to socket server');
});
}
submitChatMessage(message) {
console.log(this.socket)
this.socket.emit('chat message', message);
this.setState({chatMessage: ''});
}
render() {
return (
<View style={{flex: 1,backgroundColor:'#000000', alignItems:'center', justifyContent:'center'}}>
<Button title="Start" onPress={()=>{this.submitChatMessage('connected')}}></Button>
</View>
);
}
}
服务器
import socket
host = "192.168.0.107"
port = 15156
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
orig = (host, port)
tcp.bind(orig)
tcp.listen(1)
while True:
con, cliente = tcp.accept()
print ('Connected', cliente)
while True:
msg = con.recv(1024)
if not msg: break
print (cliente, msg)
print ('exiting', cliente)
con.close()
编辑*2:我试图捕获连接到服务器时产生的任何错误,并简单地创建了套接字对象,没有连接到服务器,没有产生错误。这怎么可能。图像中没有连接并创建了对象
componentDidMount() {
this.connectSocket()
}
connectSocket() {
try {
console.log("Connecting to the server...")
this.socket = io('http://192.168.0.107:15156',{autoConnect:false})
this.socket.connect()
console.log("Sucess");
}
catch (err){
console.log(err)
}
}
编辑*3
我在使用它时能够检测到错误,
this.socket.on("connect_error", (err) => {
console.log(err)
})
// [错误:xhr 轮询错误] //循环
重点是,最好不要找到它。
有什么前进的方向吗?
我只是忘记连接 android 网络,第三天
我正在尝试将 React Native 客户端连接到开放的 Python 服务器
问题是我阅读了几个关于如何与 socket.io-客户端 API 建立连接的解决方案,但没有任何解决办法。
就是连接不上。服务器端和客户端我都会post,改了好几次
编辑*1:服务器端工作,我使用 socket.io 和 python 套接字对其进行了测试。但是作为 React 的客户端不工作
import React,{useState, useEffect} from 'react';
import { StyleSheet, Text, View, Pressable, Button } from 'react-native';
import socketIO from "socket.io-client";
export default class App extends React.Component {
constructor(props){
super(props)
this.state = {
string:"null",
chatMessage:"null",
chatMessages:[]
}
}
componentDidMount() {
this.socket = socketIO('192.168.0.107:15156')
this.socket.connect();
this.socket.on('connect', () => {
console.log('connected to socket server');
});
}
submitChatMessage(message) {
console.log(this.socket)
this.socket.emit('chat message', message);
this.setState({chatMessage: ''});
}
render() {
return (
<View style={{flex: 1,backgroundColor:'#000000', alignItems:'center', justifyContent:'center'}}>
<Button title="Start" onPress={()=>{this.submitChatMessage('connected')}}></Button>
</View>
);
}
}
服务器
import socket
host = "192.168.0.107"
port = 15156
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
orig = (host, port)
tcp.bind(orig)
tcp.listen(1)
while True:
con, cliente = tcp.accept()
print ('Connected', cliente)
while True:
msg = con.recv(1024)
if not msg: break
print (cliente, msg)
print ('exiting', cliente)
con.close()
编辑*2:我试图捕获连接到服务器时产生的任何错误,并简单地创建了套接字对象,没有连接到服务器,没有产生错误。这怎么可能。图像中没有连接并创建了对象
componentDidMount() {
this.connectSocket()
}
connectSocket() {
try {
console.log("Connecting to the server...")
this.socket = io('http://192.168.0.107:15156',{autoConnect:false})
this.socket.connect()
console.log("Sucess");
}
catch (err){
console.log(err)
}
}
编辑*3 我在使用它时能够检测到错误,
this.socket.on("connect_error", (err) => {
console.log(err)
})
// [错误:xhr 轮询错误] //循环
重点是,最好不要找到它。 有什么前进的方向吗?
我只是忘记连接 android 网络,第三天