對(duì)于我的項(xiàng)目,我必須將一個(gè) socketIO 后端連接到另一個(gè)。為此,我使用 Flask-socketio 和 socketio-client。兩者的代碼如下:客戶:from socketIO_client import SocketIO, LoggingNamespaceip = '192.168.1.41'port = 8090def handle_aaa_response(): print('response')socketIO = SocketIO(ip, port)socketIO.on('pingSuccess', on_aaa_response)socketIO.wait(seconds=1)服務(wù)器:from flask import Flask, render_template, jsonify, Responsefrom flask_socketio import SocketIO, emitTRACE_LIBRARIES = FalseHOST = '0.0.0.0'WEB_PORT = 8090USE_PIN = Falsedef handle_connect(): print('hello world') emit('pingSuccess')app = Flask(__name__)app.config['SECRET_KEY'] = 'secret!'app.config['DEBUG'] = Truesocketio = SocketIO(app, cors_allowed_origins="*")socketio.on('connect', handle_connect)try: socketio.run(app, host=HOST, port=WEB_PORT, log_output=True)except KeyboardInterrupt: print('*** User raised KeyboardInterrupt') exit()當(dāng)我運(yùn)行客戶端和服務(wù)器時(shí),服務(wù)器僅記錄以下內(nèi)容:(4743) accepted ('192.168.1.33', 53500)192.168.1.33 - - [21/Oct/2020 15:48:31] "GET /socket.io/?EIO=3&transport=polling&t=1603291711742-0 HTTP/1.1" 200 371 0.005033(4743) accepted ('192.168.1.33', 53502)這意味著服務(wù)器正在接受來(lái)自客戶端的連接,但沒(méi)有路由到服務(wù)器上的正確路由。我想知道如何更改它,以便它到達(dá)正確的路線并打印“hello world:
2 回答

呼如林
TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊
與您在腳本中使用的socketio.on()常規(guī)包相反,用作裝飾器。socketIO_clientclientflask_socketio.on()
因此,要向 中的事件添加回調(diào)flask_socketio,您需要更改以下內(nèi)容:
...
socketio = SocketIO(app, cors_allowed_origins="*")
@socketio.on('connect')
def handle_connect():
print('hello world')
emit('pingSuccess')
...

暮色呼如
TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊
服務(wù)器端
@socketio.on('connect')
def test_connect():
print('hello world')
emit('pingSuccess')
添加回答
舉報(bào)
0/150
提交
取消