在服务器上,因为eventlet.wsgi.server(eventlet.listen(('', 5000)), app)
是阻塞的,所以下一行sio.emit('message', "hello")
不会运行。如何从服务器向客户端发送消息?我必须创建另一个线程吗?
我的服务器代码:
import socketio
import engineio
import eventlet
sio = socketio.Server()
app = socketio.WSGIApp(sio, static_files={
'/': {'content_type': 'text/html', 'filename': 'index.html'}
})
@sio.on('connect')
def connect(sid, environ):
print('connect ', sid)
@sio.on('message')
def message(sid, data):
print('message ', data)
@sio.on('disconnect')
def disconnect(sid):
print('disconnect ', sid)
if __name__ == '__main__':
eventlet.wsgi.server(eventlet.listen(('', 5000)), app)
sio.emit('message', "hello")
我的客户代码:
import socketio
sio = socketio.Client()
@sio.on('connect')
def on_connect():
print('connection established')
@sio.on('message')
def on_message(data):
print('message received with ', data)
sio.emit('my response', {'response': 'my response'})
@sio.on('disconnect')
def on_disconnect():
print('disconnected from server')
sio.connect('http://localhost:5000')
sio.emit('message',"this is my first message")