File size: 1,235 Bytes
d2db5a7
e22b961
 
7a10318
e22b961
4a70d33
e22b961
4a70d33
e22b961
 
 
 
 
 
 
 
7a10318
e22b961
 
 
 
 
 
 
 
 
 
7a10318
e22b961
 
 
 
 
 
 
 
 
 
 
84d9598
 
e22b961
 
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
40
41
42
43
44
import websockets
import asyncio
import sys

URL = "wss://ramesh-vani-collab.hf.space"

async def receiver(ws,connection):
    
        try:
          async for msg in connection:
            # msg = await connection.recv()
            print(f"\nServer: {msg}")
            await ws.send(msg)
        except websockets.exceptions.WebSocketException as e:
            print(f"WebSocket exception: {e}")
            # sys.exit(1)

async def sender(ws,connection):
    
        try:
          async for msg in ws:
            # msg = await ws.recv()
            await connection.send(msg)
        except websockets.exceptions.WebSocketException as e:
            print(f"WebSocket exception: {e}")
            # sys.exit(1)
            

async def chat(websocket, path) -> None:
    try:
        async with websockets.connect(URL) as ws:
            await asyncio.gather(
                receiver(websocket,ws),
                sender(websocket,ws)
            )
    except websockets.exceptions.WebSocketException as e:
        #print(f"WebSocket exception: {e}")
        sys.exit(1)
start_server = websockets.serve(chat,"0.0.0.0", 7860)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()