Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,34 +14,21 @@ branch_name = "scenario_sycer"
|
|
| 14 |
|
| 15 |
# WebSocket URI
|
| 16 |
ws_uri = "wss://onlinewebsocketserver.onrender.com"
|
| 17 |
-
websocket = None
|
| 18 |
|
| 19 |
-
# Connect to WebSocket
|
| 20 |
-
async def
|
| 21 |
-
|
| 22 |
try:
|
| 23 |
websocket = await websockets.connect(ws_uri)
|
| 24 |
print("Connected to the WebSocket server")
|
| 25 |
-
except Exception as e:
|
| 26 |
-
print(f"Connection failed: {e}")
|
| 27 |
-
|
| 28 |
-
async def send_data(data):
|
| 29 |
-
global websocket
|
| 30 |
-
if websocket is None or websocket.closed:
|
| 31 |
-
await connect()
|
| 32 |
-
if websocket is None or websocket.closed:
|
| 33 |
-
print("Failed to connect to WebSocket server.")
|
| 34 |
-
return
|
| 35 |
-
|
| 36 |
-
try:
|
| 37 |
await websocket.send(json.dumps(data))
|
| 38 |
print(f"Sent: {data}")
|
| 39 |
-
response = await websocket.recv()
|
| 40 |
-
response_data = json.loads(response)
|
| 41 |
-
print(f"Received: {response_data}")
|
| 42 |
except Exception as e:
|
| 43 |
-
print(f"Failed to send
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def send_graphql_query(speckleToken, apiUrl, query):
|
| 47 |
headers = {
|
|
|
|
| 14 |
|
| 15 |
# WebSocket URI
|
| 16 |
ws_uri = "wss://onlinewebsocketserver.onrender.com"
|
|
|
|
| 17 |
|
| 18 |
+
# Connect to WebSocket and send data
|
| 19 |
+
async def send_data(data):
|
| 20 |
+
websocket = None
|
| 21 |
try:
|
| 22 |
websocket = await websockets.connect(ws_uri)
|
| 23 |
print("Connected to the WebSocket server")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
await websocket.send(json.dumps(data))
|
| 25 |
print(f"Sent: {data}")
|
|
|
|
|
|
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
+
print(f"Failed to send data: {e}")
|
| 28 |
+
finally:
|
| 29 |
+
if websocket:
|
| 30 |
+
await websocket.close()
|
| 31 |
+
print("WebSocket connection closed.")
|
| 32 |
|
| 33 |
def send_graphql_query(speckleToken, apiUrl, query):
|
| 34 |
headers = {
|