websocket测试

This commit is contained in:
fengge 2026-03-22 14:27:52 +08:00
parent e1a2c95f3e
commit 09f00f8b72
3 changed files with 54 additions and 29 deletions

3
.gitignore vendored
View File

@ -14,4 +14,5 @@
.cxx .cxx
local.properties local.properties
/StandAPP/.idea /StandAPP/.idea
/StandAPP/app/release /StandAPP/app/release
/.idea/

View File

@ -29,6 +29,12 @@
.ws-status { font-size: 12px; padding: 4px 8px; border-radius: 4px; margin-left: 10px; } .ws-status { font-size: 12px; padding: 4px 8px; border-radius: 4px; margin-left: 10px; }
.ws-connected { background: #dcfce7; color: #166534; } .ws-connected { background: #dcfce7; color: #166534; }
.ws-disconnected { background: #fee2e2; color: #991b1b; } .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 { .log-container {
background: #1e293b; color: #f8fafc; padding: 10px; background: #1e293b; color: #f8fafc; padding: 10px;
border-radius: 6px; font-family: monospace; font-size: 11px; border-radius: 6px; font-family: monospace; font-size: 11px;
@ -73,6 +79,13 @@
ID: <span id="ws-client-id" style="color: #6366f1;">-</span> ID: <span id="ws-client-id" style="color: #6366f1;">-</span>
</div> </div>
<!-- WS 地址配置 -->
<div class="ws-input-group">
<label style="font-size: 12px; color: #64748b;">WebSocket 连接地址:</label>
<input type="text" id="ws-url-input" class="ws-input"
value="wss://template.gyouzhe.com/api/ws/{clientId}?Authorization=qiangfengsuiyuehen">
</div>
<div style="display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap;"> <div style="display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap;">
<button class="btn btn-green" style="margin:0; flex:1; padding: 10px; min-width: 120px;" onclick="toggleWS()">连接/断开 WS</button> <button class="btn btn-green" style="margin:0; flex:1; padding: 10px; min-width: 120px;" onclick="toggleWS()">连接/断开 WS</button>
<button class="btn" style="margin:0; flex:1; padding: 10px; min-width: 120px; background: #6366f1;" onclick="manualAddRecord()">手动新增记录</button> <button class="btn" style="margin:0; flex:1; padding: 10px; min-width: 120px; background: #6366f1;" onclick="manualAddRecord()">手动新增记录</button>
@ -242,7 +255,6 @@
// === WebSocket 逻辑 === // === WebSocket 逻辑 ===
let ws = null; let ws = null;
let wsUrl = "ws://152.32.186.199:38080/ws/";
let clientId = "WEB_" + Math.random().toString(36).substr(2, 9); let clientId = "WEB_" + Math.random().toString(36).substr(2, 9);
let heartbeatTimer = null; let heartbeatTimer = null;
let reconnectTimer = null; let reconnectTimer = null;
@ -280,37 +292,49 @@
} }
function connectWS() { function connectWS() {
if (ws) ws.close(); if (ws) {
ws.onclose = null; // 清除之前的重连逻辑
ws.close();
}
addLog("正在尝试连接: " + clientId, false); const rawUrl = document.getElementById('ws-url-input').value;
ws = new WebSocket(wsUrl + clientId); const finalUrl = rawUrl.replace("{clientId}", clientId);
addLog("正在尝试连接: " + finalUrl, false);
try {
ws = new WebSocket(finalUrl);
ws.onopen = () => { ws.onopen = () => {
updateWSStatus(true); updateWSStatus(true);
addLog("连接成功", false); addLog("连接成功", false);
startHeartbeat(); startHeartbeat();
clearTimeout(reconnectTimer); clearTimeout(reconnectTimer);
}; };
ws.onmessage = (evt) => { ws.onmessage = (evt) => {
addLog("收到消息: " + evt.data); addLog("收到消息: " + evt.data);
// 如果收到打印指令,可以自动触发打印 // 如果收到打印指令,可以自动触发打印
if (evt.data.includes("print")) { if (evt.data.includes("print")) {
callRawPrint(); callRawPrint();
} }
}; };
ws.onclose = () => { ws.onclose = () => {
updateWSStatus(false); updateWSStatus(false);
addLog("连接关闭", false); addLog("连接关闭", false);
stopHeartbeat(); stopHeartbeat();
// 5秒后自动重连 // 5秒后自动重连
reconnectTimer = setTimeout(connectWS, 5000); reconnectTimer = setTimeout(connectWS, 5000);
}; };
ws.onerror = () => { ws.onerror = (err) => {
addLog("连接发生错误", false); addLog("连接错误或被拒绝", false);
}; console.error(err);
};
} catch (e) {
addLog("连接失败: " + e.message, false);
}
} }
function toggleWS() { function toggleWS() {

View File

@ -32,7 +32,7 @@ class MainActivity : AppCompatActivity() {
.addJavascriptInterface("android", AndroidInterface(this)) .addJavascriptInterface("android", AndroidInterface(this))
.createAgentWeb() .createAgentWeb()
.ready() .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 和跨域 // 配置 WebView 属性以支持 SSE 和跨域
mAgentWeb?.let { agent -> mAgentWeb?.let { agent ->