|
@@ -0,0 +1,69 @@
|
|
|
|
+import asyncio
|
|
|
|
+import time
|
|
|
|
+
|
|
|
|
+import websockets
|
|
|
|
+from pynput import mouse
|
|
|
|
+from pynput.mouse import Controller
|
|
|
|
+from contextlib import suppress
|
|
|
|
+import messageClass
|
|
|
|
+
|
|
|
|
+connected_clients = set()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+async def broadcast(message):
|
|
|
|
+ if connected_clients:
|
|
|
|
+ await asyncio.wait([client.send(message) for client in connected_clients])
|
|
|
|
+
|
|
|
|
+async def register(websocket):
|
|
|
|
+ connected_clients.add(websocket)
|
|
|
|
+
|
|
|
|
+async def unregister(websocket):
|
|
|
|
+ connected_clients.remove(websocket)
|
|
|
|
+
|
|
|
|
+async def server_handler(websocket, path):
|
|
|
|
+ await register(websocket)
|
|
|
|
+ try:
|
|
|
|
+ async for message in websocket:
|
|
|
|
+ await broadcast(message)
|
|
|
|
+ finally:
|
|
|
|
+ await unregister(websocket)
|
|
|
|
+
|
|
|
|
+async def main():
|
|
|
|
+ async with websockets.serve(server_handler, "192.168.0.54", 8765):
|
|
|
|
+ print("websocket server is running...")
|
|
|
|
+ await asyncio.Future()
|
|
|
|
+
|
|
|
|
+mouse_controller = Controller()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def on_click(x, y, button, pressed):
|
|
|
|
+ if pressed:
|
|
|
|
+ message = messageClass.ClickMessage(x, y, button).getMessage()
|
|
|
|
+ broadcast(message)
|
|
|
|
+ print(time.time())
|
|
|
|
+
|
|
|
|
+def on_scroll(x, y, dx, dy):
|
|
|
|
+ message = messageClass.ScrollMessage(x, y, dx, dy).getMessage()
|
|
|
|
+ broadcast(message)
|
|
|
|
+ print(time.time())
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
+ listener = mouse.Listener(
|
|
|
|
+ on_click=on_click,
|
|
|
|
+ on_scroll=on_scroll)
|
|
|
|
+ listener.start()
|
|
|
|
+ loop = asyncio.get_event_loop()
|
|
|
|
+
|
|
|
|
+ try:
|
|
|
|
+ loop.run_until_complete(main())
|
|
|
|
+
|
|
|
|
+ except KeyboardInterrupt:
|
|
|
|
+ print("shutting down websocket server...")
|
|
|
|
+ finally:
|
|
|
|
+ tasks = asyncio.gather(*asyncio.all_tasks(loop), return_exceptions=True)
|
|
|
|
+ tasks.add_done_callback(lambda t: loop.stop())
|
|
|
|
+ tasks.cancel()
|
|
|
|
+
|
|
|
|
+ with suppress(asyncio.CancelledError):
|
|
|
|
+ loop.run_forever()
|