-
Notifications
You must be signed in to change notification settings - Fork 2
/
assistant_server.py
39 lines (31 loc) · 1.07 KB
/
assistant_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
32
33
34
35
36
37
38
39
import threading
import asyncio
import websockets
from assistant import AssistantEngine
PORT = 8765
assistant = AssistantEngine()
async def custom_loop(websocket, path):
while True:
#msg = await websocket.recv()
#print(msg)
if assistant.shared_data != None:
await websocket.send(str(assistant.shared_data))
print("Sent: {}".format(assistant.shared_data))
assistant.shared_data = None
await asyncio.sleep(1)
class WebSocketThread(threading.Thread):
def run(self):
asyncio.set_event_loop(asyncio.new_event_loop())
start_server = websockets.serve(custom_loop, "localhost", PORT)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
class AssistantThread(threading.Thread):
def run(self):
assistant.run()
# Create and start the WebSocket thread
websocket_thread = WebSocketThread()
websocket_thread.start()
assistant_thread = AssistantThread()
assistant_thread.start()
websocket_thread.join()
assistant_thread.join()