我正在做一个使用python套接字服务器发送和接收数据的应用程序。用react-native编写的应用程序。每当我连接并尝试发送/接收数据时,都会出现该错误:GET /socket.io/?EIO=3&transport=polling&t=Mlquqm0 HTTP / 1.1
实际上ı发送了诸如“ hello服务器”之类的数据,但服务器收到了该错误。
在ubuntu服务器中工作的python套接字。我在python客户端上尝试了此方法,它可以正常工作,但没有react-native客户端。
server.py:
import socket
import sys
import ast
import os
import time
HOST = ip
PORT = port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# socket.socket: must use to create a socket.
# socket.AF_INET: Address Format, Internet = IP Addresses.
# socket.SOCK_STREAM: two-way, connection-based byte streams.
print('socket created')
# Bind socket to Host and Port
try:
s.bind((HOST, PORT))
except socket.error as err:
print
'Bind Failed, Error Code: ' + str(err[0]) + ', Message: ' + err[1]
sys.exit()
print('Socket Bind Success!')
# listen(): This method sets up and start TCP listener.
s.listen(10)
print('Socket is now listening')
while 1:
conn, addr = s.accept()
print('Connect with ' + addr[0] + ':' + str(addr[1]))
buf = conn.recv(64)
buf = buf.decode()
command = str(buf)
print(command)
nickname,password,command = command.split()
gazoz = str(nickname) + " " + str(password) + " " + str(command)
input = open("input.txt","a+",encoding="utf-8")
input.write(str(gazoz) + "\n")
input.close()
print(nickname)
time.sleep(2)
if os.path.isfile("connection/us_" + nickname + ".txt"):
data = open("connection/us_" + nickname + ".txt","r",encoding="utf-8")
msg = data.read()
print(msg)
data.close()
os.remove("connection/us_" + nickname + ".txt")
msg = str.encode(msg)
if len(msg) == 0:
msg = "pek bisi yok"
msg = str.encode(msg)
conn.send(msg)
s.close()
反应本地客户端
import io from 'socket.io-client';
const socket = io("ip:port");
socket.connect();
socket.emit("hello server");
socket.close();
答案 0 :(得分:0)
你可以试试吗?
import SocketIOClient from "socket.io-client/dist/socket.io";
const socket = await SocketIOClient(url);
socket.on("connect", () => { console.log("connected") });