添加现有代码V1.6
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
// WebSocket连接和消息处理模块
|
||||
const topic_control = '/dev/control';
|
||||
const topic_status = '/dev/status';
|
||||
const topic_response = '/dev/response';
|
||||
const topic_scanner_status = '/dev/status/scanner';
|
||||
const topic_scanner_data = '/dev/data/scanner';
|
||||
|
||||
let ws;
|
||||
let reconnectTimer;
|
||||
let scannerStateHandler = null;
|
||||
let scannerDataHandler = null;
|
||||
let responseHandler = null;
|
||||
|
||||
// 替换 client.publish 为 ws.send
|
||||
function wsPublish(topic, msg) {
|
||||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify({
|
||||
action: 'publish',
|
||||
topic: topic,
|
||||
message: msg
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
function connectWebSocket() {
|
||||
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const wsHost = location.hostname + (location.port ? ':' + location.port : '');
|
||||
const wsuri = wsProtocol + '//' + wsHost + '/ws';
|
||||
console.log('WebSocket URI:', wsuri);
|
||||
|
||||
ws = new WebSocket(wsuri);
|
||||
|
||||
ws.onopen = function() {
|
||||
showToast('已连接到WebSocket服务器');
|
||||
ws.send(JSON.stringify({ action: 'subscribe', topic: topic_status }));
|
||||
ws.send(JSON.stringify({ action: 'subscribe', topic: topic_scanner_status }));
|
||||
// 清除重连定时器
|
||||
if (reconnectTimer) {
|
||||
clearTimeout(reconnectTimer);
|
||||
reconnectTimer = null;
|
||||
}
|
||||
};
|
||||
|
||||
ws.onmessage = function(event) {
|
||||
let data;
|
||||
//处理二进制数据
|
||||
if (event.data instanceof Blob) {
|
||||
console.log('收到二进制数据:', event.data.size);
|
||||
//处理Blob数据
|
||||
const reader = new FileReader();
|
||||
reader.onloadend = function() {
|
||||
const arrayBuffer = reader.result;
|
||||
console.log('ArrayBuffer:', arrayBuffer);
|
||||
if (scannerDataHandler) {
|
||||
scannerDataHandler(topic_scanner_data, arrayBuffer);
|
||||
}
|
||||
};
|
||||
reader.readAsArrayBuffer(event.data);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
data = JSON.parse(event.data);
|
||||
} catch (e) {
|
||||
console.log('原始消息:', typeof event.data, event.data);
|
||||
showToast('解析消息失败: ' + e.message);
|
||||
return;
|
||||
}
|
||||
const topic = data.topic;
|
||||
const message = data.message;
|
||||
|
||||
if (topic && message) {
|
||||
if (topic.startsWith(topic_response)) {
|
||||
if (responseHandler) {
|
||||
responseHandler(topic, message);
|
||||
}
|
||||
} else if (topic.startsWith(topic_scanner_status)) {
|
||||
if (scannerStateHandler) {
|
||||
scannerStateHandler(topic, message);
|
||||
}
|
||||
} else if (topic.startsWith(topic_scanner_data)) {
|
||||
if (scannerDataHandler) {
|
||||
scannerDataHandler(topic, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ws.onerror = function() {
|
||||
showToast('WebSocket连接错误');
|
||||
};
|
||||
|
||||
ws.onclose = function() {
|
||||
showToast('WebSocket连接已关闭,正在尝试重连...');
|
||||
// 仅在没有活跃定时器时创建新的重连定时器
|
||||
if (!reconnectTimer) {
|
||||
reconnectTimer = setTimeout(connectWebSocket, 5000);
|
||||
}
|
||||
};
|
||||
|
||||
return ws;
|
||||
}
|
||||
|
||||
// 设置扫描器状态处理器
|
||||
function setScannerStateHandler(handler) {
|
||||
scannerStateHandler = handler;
|
||||
}
|
||||
|
||||
// 设置扫描器数据处理器
|
||||
function setScannerDataHandler(handler) {
|
||||
scannerDataHandler = handler;
|
||||
}
|
||||
|
||||
// 设置响应处理器
|
||||
function setResponseHandler(handler) {
|
||||
responseHandler = handler;
|
||||
}
|
||||
|
||||
// 初始化WebSocket连接
|
||||
function initWebSocket() {
|
||||
connectWebSocket();
|
||||
|
||||
// 定时发送状态请求
|
||||
setInterval(function() {
|
||||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||
wsPublish(topic_control, {cmd: 'status'});
|
||||
}
|
||||
}, 500);
|
||||
}
|
||||
|
||||
// 获取WebSocket实例
|
||||
function getWebSocket() {
|
||||
return ws;
|
||||
}
|
||||
|
||||
// 导出模块
|
||||
export {
|
||||
wsPublish,
|
||||
initWebSocket,
|
||||
setScannerStateHandler,
|
||||
setScannerDataHandler,
|
||||
setResponseHandler,
|
||||
getWebSocket,
|
||||
topic_control,
|
||||
topic_status,
|
||||
topic_response,
|
||||
topic_scanner_status
|
||||
};
|
||||
Reference in New Issue
Block a user