Package: c-websockets
wsWebSocket implementation in Python built on top of websockets python library. Similar to Node.js's ws.Basic usage.server.pyimportwsserver=ws.ServerSocket()@server.on('ready')asyncdefon_ready():print(f"Server is ready listening at ws://{server.address}:{server.port}")@server.on('connect')asyncdefon_connect(client,path):print(f"Client at{client.remote_address}connected.")awaitclient.send(data={'status':"Okay","alive":True,"ping":10.4})@server.on('message')asyncdefon_message(message):print(f"{message.data}")print(f"Received from:{message.author.remote_address}at{message.created_at}")@server.on('disconnect')asyncdefon_disconnect(client,code,reason):print(f"Client at{client.remote_address}disconnected with code: ",code,"and reason: ",reason)print(server.disconnected_clients)@server.on("close")asyncdefon_close(client,code,reason):print(f"Client at{client.remote_address}closed connection with code:{code}and reason:{reason}")server.listen("localhost",3000)client.pyimportwsclient=ws.ClientSocket()@client.on('connect')asyncdefon_connect():print(f"Connected to{client.connection.remote_address}")print(client.connection)@client.on('message')asyncdefon_message(message):print(f'{message.data}')print(f'Status:{message.data.status}Alive:{message.data.alive}Ping:{message.data.ping}')print(f"Received from:{message.author.remote_address}at{message.created_at}")awaitmessage.author.send(content="Okay received.")@client.on('disconnect')asyncdefon_disconnect(code,reason):print(f"{client.connection}disconnect with code: ",code,"and reason: ",reason)print(client.disconnection)@client.on("close")asyncdefon_close(code,reason):print(f"{client.connection.remote_address}closed connection with code:{code}and reason:{reason}")client.connect("ws://localhost:3000")Event collector (.wait_for method) example.server.pyimportwsimportasyncioserver=ws.ServerSocket()@server.on('ready')asyncdefon_ready():print(f"Server is ready listening at ws://{server.address}:{server.port}")@server.on('connect')asyncdefon_connect(websocket,path):print(f"Client at{websocket.remote_address}connected.")@server.on('message')asyncdefon_message(message):print(f"{message.data}")ifmessage.data.startswith("!confirm"):awaitmessage.author.send("Send yes or no?")try:conmes=awaitserver.wait_for('message',timeout=300,check=lambdarm:rm.data.lower().strip()in['yes','no']andrm.author.remote_address==message.author.remote_address)print(conmes.data)ifconmes.data.lower().strip()=='yes':awaitconmes.author.send("done")else:awaitconmes.author.send("not done")exceptasyncio.TimeoutError:awaitmessage.author.send("Timed out!")@server.on('disconnect')asyncdefon_disconnect(client,code,reason):print(f"{client}disconnect with code:",code,reason)print(server.disconnected_clients)@server.on("close")asyncdefon_close(client,code,reason):print(f"{client.remote_address}closed connection with code:{code}and reason:{reason}")server.listen("localhost",3000)client.pyimportwsimportasyncioclient=ws.ClientSocket()@client.on('connect')asyncdefon_connect():print(f"Connected to{client.connection.remote_address}")awaitclient.send(content="!confirm")@client.on('message')asyncdefon_message(message):print(f'{message.data}')ifmessage.datain['done','not done']:returnawaitasyncio.sleep(3)awaitmessage.author.send("This is a random message. This won't be collected by the event collector on the server side due to the check condition.")awaitasyncio.sleep(3)awaitmessage.author.send("yes")#this will be collected and you would receive a response "done" for this, provide "no" and you will get "not done" response@client.on('disconnect')asyncdefon_disconnect(code,reason):print(f"{client.connection}disconnect with code:",code,reason)print(client.disconnection)@client.on("close")asyncdefon_close(code,reason):print(f"{client.connection.remote_address}closed connection with code:{code}and reason:{reason}")client.connect("ws://localhost:3000")FAQWhat's the difference betweenon_disconnectandon_closeevent listeners ?The difference isn't huge but it is there.on_disconnectevent is fired only if the client or the serverdid notclosed the connection properly, the TCP Connection was lost suddenly raising the websockets' ConnectionClosedError in the libraries internal message consumer. In such cases the code returned is usually1006and there is no reason. Whileon_closeevent listener is called if the connection was closed properly, that could be done by calling the libraries' ClientSocket or ServerSocket's.closemethod and providing a reason and error code optionally. This causes the internal message consumer task on both sides to exit / return that in turn calls the attached listener functions.Note:Unlikeon_disconnectlistener,on_closeis called on both sides that is both on the client and server websocket sides with the same reason and code.Why this exists?I made this library because I was fed up of websockets library because it didn't have event based communication like Node.js's ws library and that made it difficult to work about it. Before I was doubtful whether I would work on it anymore or not... but it has went on to become a pretty large library but still not well known as of now...