Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import websockets
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
print(f"New connection from {
|
|
|
|
| 7 |
try:
|
| 8 |
while True:
|
| 9 |
-
#
|
| 10 |
message = await websocket.recv()
|
| 11 |
-
print(f"Received: {message}")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
await websocket.send(f"
|
| 15 |
except websockets.ConnectionClosed:
|
| 16 |
-
print(f"Connection closed from {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
asyncio.run(
|
|
|
|
| 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())
|