43 lines
No EOL
1.2 KiB
Python
43 lines
No EOL
1.2 KiB
Python
import asyncio
|
|
import websockets
|
|
|
|
socket_connections = {}
|
|
|
|
async def handle_client(websocket, path):
|
|
global socket_connections
|
|
|
|
print("packet connected")
|
|
socket_connections[websocket] = ""
|
|
|
|
internal_count = 0
|
|
show_4 = False
|
|
|
|
try:
|
|
while True:
|
|
print("waiting for data")
|
|
data = await websocket.recv()
|
|
if data == "Type?":
|
|
await websocket.send("Automated")
|
|
else:
|
|
internal_count += 1
|
|
if internal_count > 100:
|
|
show_4 = not show_4
|
|
internal_count = 1
|
|
if show_4:
|
|
await websocket.send("[1] 100:100 200:700\n[2] 250:250 750:750\n[3] 500:300 800:800\n[4] 50:150 300:600")
|
|
else:
|
|
await websocket.send("[1] 100:100 200:700\n[2] 250:250 750:750\n[3] 500:300 800:800")
|
|
except websockets.exceptions.ConnectionClosed:
|
|
print("Connection closed with: ", websocket.remote_address)
|
|
finally:
|
|
print("closing websocket")
|
|
del socket_connections[websocket]
|
|
|
|
async def main():
|
|
server = await websockets.serve(handle_client, "localhost", 6543)
|
|
|
|
print("server started")
|
|
|
|
await server.wait_closed()
|
|
|
|
asyncio.run(main()) |