File size: 1,227 Bytes
d2db5a7
 
7a10318
 
 
2605416
 
7a10318
 
2605416
7a10318
2605416
7a10318
10b984e
 
7a10318
cb65892
2605416
26bfbb3
7a10318
cb65892
2605416
7a10318
 
2605416
 
 
 
 
 
 
 
7a10318
 
 
 
2605416
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
import asyncio
import websockets

async def run_code(websocket, path):
    try:
        print("WebSocket connection established.")
        
        # Read the code from the WebSocket connection
        code = await websocket.recv()
        print("Received code:", code)

        # Establish connection to external WebSocket server
        async with websockets.connect('wss://ramesh-vani-wspython.hf.space') as web:
            print("Connection to external WebSocket server established.")
            
            await web.send(code)
            print("Code sent to external WebSocket server.")

            while True:
                response = await web.recv()
                print("Received response:", response)
                await send_message(websocket, response)

    except Exception as e:
        print(f'Error: {str(e)}')
        await websocket.send(f'Error: {str(e)}')
    finally:
        print("Closing WebSocket connection.")
        await websocket.close()

async def send_message(websocket, message):
    await websocket.send(f'data: {message}')

start_server = websockets.serve(run_code, "0.0.0.0", 7860)

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