Compare commits
No commits in common. "19009fabcd40021a319a55068f2ca5f5c98286a2" and "760d43ab8a8b0788927f237c41644471fdef7534" have entirely different histories.
19009fabcd
...
760d43ab8a
@ -1,23 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
|
|
||||||
from websockets.asyncio.client import connect
|
|
||||||
import websockets
|
|
||||||
|
|
||||||
async def init():
|
|
||||||
uri = "ws://192.168.6.101:3003"
|
|
||||||
async with connect(uri) as websocket:
|
|
||||||
await websocket.send("status")
|
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
msg = await websocket.recv()
|
|
||||||
print(msg)
|
|
||||||
except websockets.ConnectionClosed:
|
|
||||||
print(f"Terminated by server")
|
|
||||||
break
|
|
||||||
except asyncio.exceptions.CancelledError:
|
|
||||||
print("Closed by user")
|
|
||||||
break
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
asyncio.run(init())
|
|
@ -1,46 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
from websockets.asyncio.server import serve
|
|
||||||
import websockets as ws
|
|
||||||
import time
|
|
||||||
|
|
||||||
class SpeakerClient:
|
|
||||||
def __init__(self, connection, instance) -> None:
|
|
||||||
self.connection = connection
|
|
||||||
self.instance = instance
|
|
||||||
self.alive = True
|
|
||||||
|
|
||||||
async def check_open(self):
|
|
||||||
try:
|
|
||||||
await self.connection.send("")
|
|
||||||
return True
|
|
||||||
except ws.ConnectionClosed:
|
|
||||||
return False
|
|
||||||
|
|
||||||
CONNECTIONS = set()
|
|
||||||
|
|
||||||
|
|
||||||
async def handler(websocket):
|
|
||||||
async for message in websocket:
|
|
||||||
args = message.split('/')
|
|
||||||
if (args[0] == "speaker"):
|
|
||||||
CONNECTIONS.add(SpeakerClient(websocket, int(args[1])))
|
|
||||||
print(f"New Speaker, instance {args[1]}")
|
|
||||||
if (args[0] == "status"):
|
|
||||||
print("Checking system health...")
|
|
||||||
remove_conns = []
|
|
||||||
for c in CONNECTIONS:
|
|
||||||
status = await c.check_open()
|
|
||||||
if status:
|
|
||||||
print(f"Instance {c.instance} is alive")
|
|
||||||
await websocket.send(f"Instance {c.instance} is alive")
|
|
||||||
else:
|
|
||||||
remove_conns.append(c)
|
|
||||||
for c in remove_conns:
|
|
||||||
CONNECTIONS.remove(c)
|
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
async with serve(handler, "", 3003):
|
|
||||||
await asyncio.get_running_loop().create_future() # run forever
|
|
||||||
|
|
||||||
asyncio.run(main())
|
|
@ -1,20 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
|
|
||||||
from websockets.asyncio.client import connect
|
|
||||||
import websockets
|
|
||||||
|
|
||||||
async def init():
|
|
||||||
uri = "ws://192.168.6.101:3003"
|
|
||||||
async with connect(uri) as websocket:
|
|
||||||
await websocket.send("speaker/2")
|
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
await websocket.recv()
|
|
||||||
except websockets.ConnectionClosed:
|
|
||||||
print(f"Terminated by server")
|
|
||||||
break
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
asyncio.run(init())
|
|
Loading…
Reference in New Issue
Block a user