我在连接 flask 插座时遇到了一些问题。
由于某些原因,当我的客户端尝试连接到服务器时,他会收到以下错误/输出:
Traceback (most recent call last):
File "C:/Users/PycharmProjects/learn/check.py", line 28, in <module>
main()
File "C:/Users/PycharmProjects/learn/check.py", line 21, in main
sio.connect('http://localhost:5000')
File "C:\Users\PycharmProjects\learn\venv\lib\site-packages\socketio\client.py", line 309, in connect
'One or more namespaces failed to connect')
socketio.exceptions.ConnectionError: One or more namespaces failed to connect
I received a message!
bruh
connected
sent them
服务器输出:
127.0.0.1 - - [27/Apr/2021 15:35:27] "GET /socket.io/?transport=polling&EIO=4&t=1619526925.9191115 HTTP/1.1" 200 -
127.0.0.1 - - [27/Apr/2021 15:35:30] "POST /socket.io/?transport=polling&EIO=4&sid=5Lmu1kce4ijdRLzQAAAA HTTP/1.1" 200 -
127.0.0.1 - - [27/Apr/2021 15:35:30] "GET /socket.io/?transport=polling&EIO=4&sid=5Lmu1kce4ijdRLzQAAAA&t=1619526927.9800713 HTTP/1.1" 200 -
connected
erere
客户代码:
import asyncio
import sys
import socketio
sio = socketio.Client()
@sio.event
def connect():
print('connected')
sio.send("bruhhuhuhuhuhuhuh")
sio.send("message")
print("sent them")
@sio.event
def message(data):
print('I received a message!')
print(data)
def main():
sio.connect('http://localhost:5000')
sio.send("message")
sio.wait()
print("test")
if __name__ == '__main__':
main()
socketio的服务器部分:
from flask_socketio import SocketIO, send, emit
app = Flask(__name__, static_url_path='/static')
socketio = SocketIO(app, cors_allowed_origins="https://amritb.github.io")
@socketio.event
def message(data):
print(data)
@socketio.on('json')
def handle_json(json):
print('received json: ' + str(json))
@socketio.on('connect')
def test_connect():
print("connected")
send("bruh")
print("erere")
if __name__ == '__main__':
socketio.run(app)
app.run(debug=True)
我犯了什么错误导致错误出现?
暂无答案!
目前还没有任何答案,快来回答吧!