socketio.send() 不向客户端发送数据
socketio.send() does not send data to client
我正在尝试使用 socketIO 从服务器向 flutter 应用程序发送数据。虽然我能够连接和发出,但服务器无法将数据发送到客户端。
服务器端代码:
import cv2
import numpy as np
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from threading import Lock,Timer as tmr
from engineio.payload import Payload
import base64
import io
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
someList = ['apple', 'peas', 'juice','orange']
i=0
@socketio.on('connect')
def connect():
print("a client connected")
@socketio.on('disconnect')
def disconnect():
print('Client disconnected')
@socketio.on('msg')
def handlemsg(msg):
print (msg)
socketio.send("msg from server")
@app.route('/')
def hello():
return "hii"
if __name__ == '__main__':
socketio.run(app,host= '0.0.0.0')
客户端(抖动)
@override
void initState() {
super.initState();
IO.Socket socket = IO.io('http://x.x.x.x:5000', <String, dynamic>{
'transports': ['websocket', 'polling']});
socket.connect();
socket.emit('msg', 'test');
socket.onConnect((_) {
print('connect');
socket.emit('msg', 'testing');
});
socket.onDisconnect((_) => print('disconnect'));
socket.on('*', (data) => print(data)); //nothing is printed
}
我在服务器端得到的结果:
a client connected
testing
但是,我在客户端没有得到任何数据。我哪里错了?请帮忙
我无法用 flutter 测试它,但我用 python-socketio
创建的客户端测试了它
主要问题可能是 send()
发送名称为 "message"
的消息,例如 emit("message", ...)
,但您的 on("msg", ...)
需要名称为 "msg"
的消息,而不是 "message"
.
所以你应该在Python中使用emit("msg", ...)
,在flutter中使用on("msg", ...)
。
或者你应该在Python中使用send()
,在flutter中使用on("message", ...)
。
其他问题可能是发送和接收消息可能需要一些时间 - 连接后可能需要额外时间,断开连接前可能需要额外时间 - 至少在我的示例中我必须睡觉才能获得结果。
完整的工作代码。
我用不同的名字添加了更多 emit()
。
server.py
from flask import Flask
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@socketio.on('connect')
def connect():
print("client connected")
@socketio.on('disconnect')
def disconnect():
print('client disconnected')
@socketio.on('question')
def handle_questio(msg):
print('question msg:', msg)
socketio.emit("answer", "msg from server")
@socketio.on('help')
def handle_help(msg):
print('help msg:', msg)
socketio.emit("support", "help from server")
@app.route('/')
def hello():
return "hii"
if __name__ == '__main__':
print('start')
socketio.run(app, host='0.0.0.0')
client.py
import socketio
sio = socketio.Client()
@sio.on('connect')
def connect():
print('connected')
@sio.on('disconnect')
def disconnect():
print('disconnected')
@sio.on('answer')
def answer(data):
print('answer:', data)
@sio.on('support')
def support(data):
print('support:', data)
# --- main ---
print('start')
sio.connect('http://localhost:5000')
print('sleep')
sio.sleep(1)
print('emit question')
sio.emit('question', {'foo': 'bar'})
print('emit help')
sio.emit('help', 'can you help me')
print('sleep')
sio.sleep(1)
sio.disconnect()
我正在尝试使用 socketIO 从服务器向 flutter 应用程序发送数据。虽然我能够连接和发出,但服务器无法将数据发送到客户端。
服务器端代码:
import cv2
import numpy as np
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from threading import Lock,Timer as tmr
from engineio.payload import Payload
import base64
import io
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
someList = ['apple', 'peas', 'juice','orange']
i=0
@socketio.on('connect')
def connect():
print("a client connected")
@socketio.on('disconnect')
def disconnect():
print('Client disconnected')
@socketio.on('msg')
def handlemsg(msg):
print (msg)
socketio.send("msg from server")
@app.route('/')
def hello():
return "hii"
if __name__ == '__main__':
socketio.run(app,host= '0.0.0.0')
客户端(抖动)
@override
void initState() {
super.initState();
IO.Socket socket = IO.io('http://x.x.x.x:5000', <String, dynamic>{
'transports': ['websocket', 'polling']});
socket.connect();
socket.emit('msg', 'test');
socket.onConnect((_) {
print('connect');
socket.emit('msg', 'testing');
});
socket.onDisconnect((_) => print('disconnect'));
socket.on('*', (data) => print(data)); //nothing is printed
}
我在服务器端得到的结果:
a client connected
testing
但是,我在客户端没有得到任何数据。我哪里错了?请帮忙
我无法用 flutter 测试它,但我用 python-socketio
主要问题可能是 send()
发送名称为 "message"
的消息,例如 emit("message", ...)
,但您的 on("msg", ...)
需要名称为 "msg"
的消息,而不是 "message"
.
所以你应该在Python中使用emit("msg", ...)
,在flutter中使用on("msg", ...)
。
或者你应该在Python中使用send()
,在flutter中使用on("message", ...)
。
其他问题可能是发送和接收消息可能需要一些时间 - 连接后可能需要额外时间,断开连接前可能需要额外时间 - 至少在我的示例中我必须睡觉才能获得结果。
完整的工作代码。
我用不同的名字添加了更多 emit()
。
server.py
from flask import Flask
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@socketio.on('connect')
def connect():
print("client connected")
@socketio.on('disconnect')
def disconnect():
print('client disconnected')
@socketio.on('question')
def handle_questio(msg):
print('question msg:', msg)
socketio.emit("answer", "msg from server")
@socketio.on('help')
def handle_help(msg):
print('help msg:', msg)
socketio.emit("support", "help from server")
@app.route('/')
def hello():
return "hii"
if __name__ == '__main__':
print('start')
socketio.run(app, host='0.0.0.0')
client.py
import socketio
sio = socketio.Client()
@sio.on('connect')
def connect():
print('connected')
@sio.on('disconnect')
def disconnect():
print('disconnected')
@sio.on('answer')
def answer(data):
print('answer:', data)
@sio.on('support')
def support(data):
print('support:', data)
# --- main ---
print('start')
sio.connect('http://localhost:5000')
print('sleep')
sio.sleep(1)
print('emit question')
sio.emit('question', {'foo': 'bar'})
print('emit help')
sio.emit('help', 'can you help me')
print('sleep')
sio.sleep(1)
sio.disconnect()