What's the problem?
I wrote a simple client and server. Client sends platform data to server, and server sends a number of connected clients. When the client disconnects nothing is being printed out (client's disconnect() function seems to be not called). I just started learning this package so it might be just me not understanding some part of it.
What I've done so far
Talk is cheap, show me the code
Server
import eventlet
import socketio
sio = socketio.Server()
app = socketio.WSGIApp(sio)
global bg_task, clients
bg_task = None
clients = dict()
def background_task():
print('Background task started!')
while True:
sio.sleep(3)
sio.emit('clients_info', f'Connected clients: {len(clients)}')
@sio.event
def connect(sid, environ):
global clients, bg_task
print('Client connected\t', sid)
clients.update({sid: environ})
if not bg_task:
sio.start_background_task(background_task)
bg_task = True
@sio.event
def sys_data(sid, data):
print(sid, data)
@sio.event
def disconnect(sid):
try:
sio.disconnect(sid)
del clients[sid]
except KeyError:
print(f"Key {sid} was not found in client list")
print('Client disconnected\t', sid)
if __name__ == '__main__':
eventlet.wsgi.server(eventlet.listen(('', 6000)), app)
Client
import socketio
import platform
import time
sio = socketio.Client()
def background_task():
# Stop the task if connection was dropped
while sio.connected:
# Send some platform data
info = f'{platform.node()} {platform.system()}'
sio.emit('sys_data', f'{time.asctime()} - sys_data - {info}')
time.sleep(1)
@sio.event
def connect():
print('Connection established')
sio.start_background_task(background_task)
@sio.on('clients_info')
def my_response(data):
print(data)
@sio.event
def disconnect():
print('Disconnected from server')
sio.connect('http://localhost:6000/')
sio.sleep(5)
sio.disconnect()
What's the problem?
I wrote a simple client and server. Client sends platform data to server, and server sends a number of connected clients. When the client disconnects nothing is being printed out (client's
disconnect()function seems to be not called). I just started learning this package so it might be just me not understanding some part of it.What I've done so far
sio.disconnect()function both from client.py and server.pyTalk is cheap, show me the code
Server
Client