ultrabotbot commited on
Commit
bf0eee3
·
verified ·
1 Parent(s): 33d0ee4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -1,25 +1,31 @@
 
1
  import asyncio
2
  import websockets
3
- print(0)
4
- # Define the WebSocket server handler
5
- async def handler(websocket, path):
6
- print(f"New connection from {websocket.remote_address}")
 
7
  try:
8
  while True:
9
- # Wait for a message from the client
10
  message = await websocket.recv()
11
- print(f"Received: {message}")
12
 
13
- # Echo the message back to the client
14
- await websocket.send(f"Echo: {message}")
15
  except websockets.ConnectionClosed:
16
- print(f"Connection closed from {websocket.remote_address}")
 
 
 
 
17
 
18
- # Start the WebSocket server
19
- async def start_server():
20
- server = await websockets.serve(handler, "0.0.0.0", 7860)
21
- print("WebSocket server started on ws://0.0.0.0:7860")
22
- await server.wait_closed()
23
 
24
- # Run the server
25
- asyncio.run(start_server())
 
1
+
2
  import asyncio
3
  import websockets
4
+
5
+ async def handle_connection(websocket, path):
6
+ """Handle a new WebSocket connection."""
7
+ print(f"New connection from {path}")
8
+
9
  try:
10
  while True:
11
+ # Receive message from client
12
  message = await websocket.recv()
13
+ print(f"Received message: {message}")
14
 
15
+ # Send response back to client
16
+ await websocket.send(f"Server received: {message}")
17
  except websockets.ConnectionClosed:
18
+ print(f"Connection closed from {path}")
19
+ except websockets.WebSocketException as e:
20
+ print(f"WebSocket error: {e}")
21
+ except Exception as e:
22
+ print(f"Error: {e}")
23
 
24
+ async def main():
25
+ """Start the WebSocket server."""
26
+ async with websockets.serve(handle_connection, "0.0.0.0", 7860):
27
+ print("Server started on port 8765")
28
+ await asyncio.Future() # Run forever
29
 
30
+ if __name__ == "__main__":
31
+ asyncio.run(main())