On the server, since eventlet.wsgi.server(eventlet.listen(('', 5000)), app)
is blocking, the next line sio.emit('message', "hello")
does not run. How do I send message from server to client? Will I have to create another thread?
My server code:
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")
My client code:
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")
You have two options:
add your emit to the connect
handler, so that it runs every time a new client connects to your server.
start a background task before you launch the server and do it from there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.