websocket测试
This commit is contained in:
parent
e1a2c95f3e
commit
09f00f8b72
|
|
@ -14,4 +14,5 @@
|
|||
.cxx
|
||||
local.properties
|
||||
/StandAPP/.idea
|
||||
/StandAPP/app/release
|
||||
/StandAPP/app/release
|
||||
/.idea/
|
||||
|
|
|
|||
|
|
@ -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: <span id="ws-client-id" style="color: #6366f1;">-</span>
|
||||
</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;">
|
||||
<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>
|
||||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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 ->
|
||||
|
|
|
|||
Loading…
Reference in New Issue