-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
37 lines (31 loc) · 1.04 KB
/
app.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
import asyncio
import websockets
import threading
import queue
# Queue to store messages
message_queue = queue.Queue()
# Function to simulate message producer
def message_producer():
while True:
message = input("Enter a message to send (or 'exit' to quit): ")
if message == 'exit':
break
message_queue.put(message)
# WebSocket server coroutine
async def websocket_server(websocket, path):
while True:
try:
# Wait for messages in the queue
message = message_queue.get()
await websocket.send(message)
except websockets.exceptions.ConnectionClosed:
break
# Starting the WebSocket server
async def start_server():
server = await websockets.serve(websocket_server, 'localhost', 8765)
await server.wait_closed()
# Starting the message producer thread
producer_thread = threading.Thread(target=message_producer)
producer_thread.start()
# Running the event loop for the WebSocket server
asyncio.get_event_loop().run_until_complete(start_server())