from easterly import WebSocketServer, ServerEvent, ServerProtocol def message(id, message): wss.broadcast(message) wss = WebSocketServer(r"/ws", 8899) wss.add_event_listener(ServerEvent.ON_MESSAGE, message) wss.start()
def on_disconnect(id): del users[id] def set_username(id, data): users[id] = data[0] def get_users(id, data): response = { 'type': 'users', 'users': users.values() } wss.whisper(id, response) def message(id, data): response = { 'type': 'msg', 'user': users[id], 'message': data[0], } wss.broadcast(response) wss = WebSocketServer(r"/ws", 8899, protocol=ServerProtocol.SEPERATOR("|")) wss.add_event_listeners([ (ServerEvent.ON_CONNECT, on_connect), (ServerEvent.ON_DISCONNECT, on_disconnect), (SET_USERNAME, set_username), (GET_USERS, get_users), (MESSAGE, message), ]) wss.start()