Ramesh-vani commited on
Commit
e22b961
1 Parent(s): 8c6a92a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -30
app.py CHANGED
@@ -1,38 +1,44 @@
1
- import threading
2
- import asyncio
3
  import websockets
 
 
 
4
 
 
5
 
6
- async def websocket_consumer(websocket, path):
7
 
8
- try:
9
- code = await websocket.recv()
10
- print(code)
11
- async with websockets.connect('wss://ramesh-vani-wspython.hf.space') as wssocket:
12
-
13
-
14
- await wssocket.send(code)
15
- print('code sent')
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
- class WebSocketThread(threading.Thread):
26
- def run(self):
27
- asyncio.set_event_loop(asyncio.new_event_loop())
28
- start_server = websockets.serve(websocket_consumer, '0.0.0.0', 7860)
29
- asyncio.get_event_loop().run_until_complete(start_server)
30
- asyncio.get_event_loop().run_forever()
 
 
 
 
31
 
32
- # websocket_thread = WebSocketThread()
33
- # websocket_thread.start()
34
- # asyncio.set_event_loop(asyncio.new_event_loop())
35
- start_server = websockets.serve(websocket_consumer, "0.0.0.0", 7860)
 
 
 
 
 
 
 
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
+