-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
32 lines (26 loc) · 1005 Bytes
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import asyncio
import websockets
import msgpack
from accessible_output2 import outputs
speaker = outputs.auto.Auto()
def parse_message(data):
try:
return msgpack.unpackb(data, raw=False)
except Exception as e:
print("Error parsing message:", e)
async def serve(websocket, path):
async for message in websocket:
message = parse_message(message)
if type(message) is str:
if message.lower() == 'cancelspeech':
speaker.output("", interrupt=True)
elif type(message) is dict:
if "BrailleMessage" in message:
text = message["BrailleMessage"]
speaker.braille(text)
elif "SpeakText" in message:
text = message["SpeakText"]
speaker.speak(text, interrupt=False)
start_server = websockets.serve(serve, "localhost", 3457, ping_timeout = None)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()