MCPcopy Create free account
hub / github.com/Tron521/codex-console-temp / connectWebSocket

Function connectWebSocket

static/js/app.js:1304–1419  ·  view source on GitHub ↗
(taskUuid)

Source from the content-addressed store, hash-verified

1302
1303// 连接 WebSocket
1304function connectWebSocket(taskUuid) {
1305 activeTaskUuid = taskUuid;
1306
1307 if (webSocket && [WebSocket.OPEN, WebSocket.CONNECTING].includes(webSocket.readyState)) {
1308 return;
1309 }
1310
1311 if (wsReconnectTimer) {
1312 clearTimeout(wsReconnectTimer);
1313 wsReconnectTimer = null;
1314 }
1315 wsManualClose = false;
1316
1317 const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
1318 const wsUrl = `${protocol}//${window.location.host}/api/ws/task/${taskUuid}`;
1319
1320 try {
1321 const socket = new WebSocket(wsUrl);
1322 webSocket = socket;
1323
1324 socket.onopen = () => {
1325 if (webSocket !== socket) return;
1326 console.log('WebSocket 连接成功');
1327 useWebSocket = true;
1328 clearWebSocketReconnect();
1329 // 停止轮询(如果有)
1330 stopLogPolling();
1331 // 开始心跳
1332 startWebSocketHeartbeat();
1333 };
1334
1335 socket.onmessage = (event) => {
1336 if (webSocket !== socket) return;
1337 const data = JSON.parse(event.data);
1338
1339 if (data.type === 'log') {
1340 const logType = getLogType(data.message);
1341 addLog(logType, data.message);
1342 } else if (data.type === 'status') {
1343 updateTaskStatus(data.status);
1344 if (data.email) {
1345 elements.taskEmail.textContent = data.email;
1346 }
1347 if (data.email_service) {
1348 elements.taskService.textContent = getServiceTypeText(data.email_service);
1349 }
1350
1351 // 检查是否完成
1352 if (['completed', 'failed', 'cancelled', 'cancelling'].includes(data.status)) {
1353 // 保存最终状态,用于 onclose 判断
1354 taskFinalStatus = data.status;
1355 taskCompleted = true;
1356
1357 // 断开 WebSocket(异步操作)
1358 disconnectWebSocket();
1359
1360 // 任务完成后再重置按钮
1361 resetButtons();

Callers 4

handleSingleRegistrationFunction · 0.85
initVisibilityReconnectFunction · 0.85
restoreActiveTaskFunction · 0.85

Calls 15

clearWebSocketReconnectFunction · 0.85
stopLogPollingFunction · 0.85
startWebSocketHeartbeatFunction · 0.85
getLogTypeFunction · 0.85
addLogFunction · 0.85
updateTaskStatusFunction · 0.85
getServiceTypeTextFunction · 0.85
disconnectWebSocketFunction · 0.85
resetButtonsFunction · 0.85
loadRecentAccountsFunction · 0.85
stopWebSocketHeartbeatFunction · 0.85
startLogPollingFunction · 0.85

Tested by

no test coverage detected