Spaces:
Runtime error
Runtime error
Ramesh-vani
commited on
Commit
•
e22b961
1
Parent(s):
8c6a92a
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,44 @@
|
|
1 |
-
import threading
|
2 |
-
import asyncio
|
3 |
import websockets
|
|
|
|
|
|
|
4 |
|
|
|
5 |
|
6 |
-
async def
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
while True:
|
17 |
-
print('loop')
|
18 |
-
response = await wssocket.recv()
|
19 |
-
print(response)
|
20 |
-
await websocket.send(response)
|
21 |
-
|
22 |
-
except websockets.exceptions.ConnectionClosedOK:
|
23 |
-
print('error')
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
asyncio.get_event_loop().run_until_complete(start_server)
|
38 |
-
asyncio.get_event_loop().run_forever()
|
|
|
|
|
|
|
|
1 |
import websockets
|
2 |
+
import asyncio
|
3 |
+
import aioconsole
|
4 |
+
import sys
|
5 |
|
6 |
+
URL = "wss://ramesh-vani-collab.hf.space"
|
7 |
|
8 |
+
async def receiver(ws,connection):
|
9 |
|
10 |
+
try:
|
11 |
+
async for msg in connection:
|
12 |
+
# msg = await connection.recv()
|
13 |
+
print(f"\nServer: {msg}")
|
14 |
+
await ws.send(msg)
|
15 |
+
except websockets.exceptions.WebSocketException as e:
|
16 |
+
print(f"WebSocket exception: {e}")
|
17 |
+
# sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
async def sender(ws,connection):
|
20 |
+
|
21 |
+
try:
|
22 |
+
async for msg in ws:
|
23 |
+
# msg = await ws.recv()
|
24 |
+
await connection.send(msg)
|
25 |
+
except websockets.exceptions.WebSocketException as e:
|
26 |
+
print(f"WebSocket exception: {e}")
|
27 |
+
# sys.exit(1)
|
28 |
+
|
29 |
|
30 |
+
async def chat(websocket, path) -> None:
|
31 |
+
try:
|
32 |
+
async with websockets.connect(URL) as ws:
|
33 |
+
await asyncio.gather(
|
34 |
+
receiver(websocket,ws),
|
35 |
+
sender(websocket,ws)
|
36 |
+
)
|
37 |
+
except websockets.exceptions.WebSocketException as e:
|
38 |
+
#print(f"WebSocket exception: {e}")
|
39 |
+
sys.exit(1)
|
40 |
+
start_server = websockets.serve(chat,"0.0.0.0", 7860)
|
41 |
|
42 |
asyncio.get_event_loop().run_until_complete(start_server)
|
43 |
+
asyncio.get_event_loop().run_forever()
|
44 |
+
|