Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1207,6 +1207,7 @@ async def start(websocket,events):
|
|
1207 |
user = User()
|
1208 |
connected = {websocket}
|
1209 |
process_ids = []
|
|
|
1210 |
|
1211 |
join_key = secrets.token_urlsafe(12)
|
1212 |
JOIN[join_key] = user, connected
|
@@ -1261,22 +1262,34 @@ async def handler(websocket):
|
|
1261 |
|
1262 |
"""
|
1263 |
# Receive and parse the "init" event from the UI.
|
1264 |
-
|
1265 |
-
|
1266 |
-
|
1267 |
-
|
1268 |
-
|
1269 |
-
|
1270 |
-
|
1271 |
-
|
1272 |
-
|
1273 |
-
|
1274 |
-
|
1275 |
-
|
1276 |
-
|
1277 |
-
|
1278 |
-
|
1279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1280 |
|
1281 |
async def main():
|
1282 |
# Set the stop condition when receiving SIGTERM.
|
|
|
1207 |
user = User()
|
1208 |
connected = {websocket}
|
1209 |
process_ids = []
|
1210 |
+
|
1211 |
|
1212 |
join_key = secrets.token_urlsafe(12)
|
1213 |
JOIN[join_key] = user, connected
|
|
|
1262 |
|
1263 |
"""
|
1264 |
# Receive and parse the "init" event from the UI.
|
1265 |
+
try:
|
1266 |
+
message_parts = {}
|
1267 |
+
async for message in websocket:
|
1268 |
+
data = json.loads(message)
|
1269 |
+
msg_id = data['msg_id']
|
1270 |
+
part = data['part']
|
1271 |
+
|
1272 |
+
if msg_id not in message_parts:
|
1273 |
+
message_parts[msg_id] = []
|
1274 |
+
message_parts[msg_id].append(part)
|
1275 |
+
|
1276 |
+
if data['end']:
|
1277 |
+
full_message = ''.join(message_parts[msg_id])
|
1278 |
+
print(f"Received full message: {full_message}")
|
1279 |
+
|
1280 |
+
event = json.loads(full_message)
|
1281 |
+
|
1282 |
+
if event["type"] == "init":
|
1283 |
+
if "join" in event:
|
1284 |
+
# Second player joins an existing game.
|
1285 |
+
await join(websocket, event["join"])
|
1286 |
+
else:
|
1287 |
+
# First player starts a new game.
|
1288 |
+
await start(websocket, full_message)
|
1289 |
+
elif event["type"] == "cmd":
|
1290 |
+
# print('executing commad')
|
1291 |
+
# Execute a command in the project folder.
|
1292 |
+
await execute_command(websocket, event["project_name"], event["command"])
|
1293 |
|
1294 |
async def main():
|
1295 |
# Set the stop condition when receiving SIGTERM.
|