From 09f00f8b72830ddddfbd3c204b8ab091036cd739 Mon Sep 17 00:00:00 2001 From: fengge <844143714@qq.com> Date: Sun, 22 Mar 2026 14:27:52 +0800 Subject: [PATCH] =?UTF-8?q?websocket=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- StandAPP/app/src/main/assets/print_demo.html | 78 ++++++++++++------- .../java/com/stand/standapp/MainActivity.kt | 2 +- 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index fa9b0cd..b427032 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ .cxx local.properties /StandAPP/.idea -/StandAPP/app/release \ No newline at end of file +/StandAPP/app/release +/.idea/ diff --git a/StandAPP/app/src/main/assets/print_demo.html b/StandAPP/app/src/main/assets/print_demo.html index f269037..93eb261 100644 --- a/StandAPP/app/src/main/assets/print_demo.html +++ b/StandAPP/app/src/main/assets/print_demo.html @@ -29,6 +29,12 @@ .ws-status { font-size: 12px; padding: 4px 8px; border-radius: 4px; margin-left: 10px; } .ws-connected { background: #dcfce7; color: #166534; } .ws-disconnected { background: #fee2e2; color: #991b1b; } + .ws-input-group { margin-top: 10px; display: flex; flex-direction: column; gap: 5px; } + .ws-input { + padding: 10px; border: 1px solid #cbd5e1; border-radius: 6px; + font-size: 12px; font-family: monospace; width: 100%; box-sizing: border-box; + background: #f8fafc; color: #334155; + } .log-container { background: #1e293b; color: #f8fafc; padding: 10px; border-radius: 6px; font-family: monospace; font-size: 11px; @@ -73,6 +79,13 @@ ID: - + +
+ + +
+
@@ -242,7 +255,6 @@ // === WebSocket 逻辑 === let ws = null; - let wsUrl = "ws://152.32.186.199:38080/ws/"; let clientId = "WEB_" + Math.random().toString(36).substr(2, 9); let heartbeatTimer = null; let reconnectTimer = null; @@ -280,37 +292,49 @@ } function connectWS() { - if (ws) ws.close(); + if (ws) { + ws.onclose = null; // 清除之前的重连逻辑 + ws.close(); + } - addLog("正在尝试连接: " + clientId, false); - ws = new WebSocket(wsUrl + clientId); + const rawUrl = document.getElementById('ws-url-input').value; + const finalUrl = rawUrl.replace("{clientId}", clientId); + + addLog("正在尝试连接: " + finalUrl, false); + + try { + ws = new WebSocket(finalUrl); - ws.onopen = () => { - updateWSStatus(true); - addLog("连接成功", false); - startHeartbeat(); - clearTimeout(reconnectTimer); - }; + ws.onopen = () => { + updateWSStatus(true); + addLog("连接成功", false); + startHeartbeat(); + clearTimeout(reconnectTimer); + }; - ws.onmessage = (evt) => { - addLog("收到消息: " + evt.data); - // 如果收到打印指令,可以自动触发打印 - if (evt.data.includes("print")) { - callRawPrint(); - } - }; + ws.onmessage = (evt) => { + addLog("收到消息: " + evt.data); + // 如果收到打印指令,可以自动触发打印 + if (evt.data.includes("print")) { + callRawPrint(); + } + }; - ws.onclose = () => { - updateWSStatus(false); - addLog("连接关闭", false); - stopHeartbeat(); - // 5秒后自动重连 - reconnectTimer = setTimeout(connectWS, 5000); - }; + ws.onclose = () => { + updateWSStatus(false); + addLog("连接关闭", false); + stopHeartbeat(); + // 5秒后自动重连 + reconnectTimer = setTimeout(connectWS, 5000); + }; - ws.onerror = () => { - addLog("连接发生错误", false); - }; + ws.onerror = (err) => { + addLog("连接错误或被拒绝", false); + console.error(err); + }; + } catch (e) { + addLog("连接失败: " + e.message, false); + } } function toggleWS() { diff --git a/StandAPP/app/src/main/java/com/stand/standapp/MainActivity.kt b/StandAPP/app/src/main/java/com/stand/standapp/MainActivity.kt index fa6e5ae..2bdb293 100644 --- a/StandAPP/app/src/main/java/com/stand/standapp/MainActivity.kt +++ b/StandAPP/app/src/main/java/com/stand/standapp/MainActivity.kt @@ -32,7 +32,7 @@ class MainActivity : AppCompatActivity() { .addJavascriptInterface("android", AndroidInterface(this)) .createAgentWeb() .ready() - .go("http://152.32.186.199/local/print_demo.html?t=\${System.currentTimeMillis()}") + .go("https://template.gyouzhe.com/example/print_demo.html?t=\${System.currentTimeMillis()}") // 配置 WebView 属性以支持 SSE 和跨域 mAgentWeb?.let { agent ->