优化长连接
This commit is contained in:
parent
de8c5e20f4
commit
d101ecea41
|
|
@ -1,171 +1 @@
|
||||||
"use strict";
|
"use strict";const i=require("path"),e=require("electron");let a;e.app.whenReady().then(()=>{a=new e.BrowserWindow({title:"银收客",width:1024,height:768,fullscreenable:!0,fullscreen:!process.env.VITE_DEV_SERVER_URL,simpleFullscreen:!0,frame:!!process.env.VITE_DEV_SERVER_URL,webPreferences:{nodeIntegration:!0,contextIsolation:!1}}),process.env.VITE_DEV_SERVER_URL?a.loadURL(process.env.VITE_DEV_SERVER_URL):a.loadFile(i.resolve(__dirname,"../dist/index.html")),e.app.on("activate",()=>{e.BrowserWindow.getAllWindows().length===0&&createWindow()}),e.ipcMain.on("quitHandler",(t,n)=>{e.app.quit()}),e.ipcMain.on("getPrintList",()=>{a.webContents.getPrintersAsync().then(t=>{a.webContents.send("printList",t)})});const o=new e.BrowserWindow({show:!1,width:464,height:1726,webPreferences:{nodeIntegration:!0,contextIsolation:!1}});process.env.VITE_DEV_SERVER_URL?o.loadFile(i.join(__dirname,"../public/print.html")):o.loadFile(i.resolve(__dirname,"../dist/print.html")),e.ipcMain.on("printerInfoSync",(t,n)=>{o.webContents.send("getParams",n)}),e.ipcMain.on("printStart",(t,n)=>{console.log(n);let r=JSON.parse(n).deviceName;o.webContents.print({silent:!0,deviceName:r,pageSize:{width:58e3,height:216e3},scaleFactor:80,landscape:!1,margins:{marginType:"none",top:0,bottom:0,left:0,right:0},dpi:{horizontal:203,vertical:203}})});const s=new e.BrowserWindow({show:!1,width:464,height:1726,webPreferences:{nodeIntegration:!0,contextIsolation:!1}});process.env.VITE_DEV_SERVER_URL?s.loadFile(i.join(__dirname,"../public/work_print.html")):s.loadFile(i.resolve(__dirname,"../dist/work_print.html")),e.ipcMain.on("printerWorkSync",(t,n)=>{s.webContents.send("getParams",n)}),e.ipcMain.on("printWorkStart",(t,n)=>{let r=JSON.parse(n).deviceName;s.webContents.print({silent:!0,deviceName:r,pageSize:{width:58e3,height:216e3},scaleFactor:80,landscape:!1,margins:{marginType:"none",top:0,bottom:0,left:0,right:0},dpi:{horizontal:203,vertical:203}})});const l=new e.BrowserWindow({show:!1,width:320,height:240,webPreferences:{nodeIntegration:!0,contextIsolation:!1}});process.env.VITE_DEV_SERVER_URL?l.loadFile(i.join(__dirname,"../public/tag_print.html")):l.loadFile(i.resolve(__dirname,"../dist/tag_print.html")),e.ipcMain.on("printerTagSync",(t,n)=>{console.log(n),l.webContents.send("getParams",n)}),e.ipcMain.on("printTagStart",(t,n)=>{let r=JSON.parse(n).deviceName;l.webContents.print({silent:!0,deviceName:r,pageSize:{width:4e4,height:3e4},scaleFactor:80,landscape:!1,margins:{marginType:"none",top:0,bottom:0,left:0,right:0},dpi:{horizontal:203,vertical:203}})})});e.app.on("window-all-closed",()=>{process.platform!=="darwin"&&e.app.quit()});
|
||||||
const path = require("path");
|
|
||||||
const electron = require("electron");
|
|
||||||
let win;
|
|
||||||
electron.app.whenReady().then(() => {
|
|
||||||
win = new electron.BrowserWindow({
|
|
||||||
title: "银收客",
|
|
||||||
width: 1024,
|
|
||||||
height: 768,
|
|
||||||
fullscreenable: true,
|
|
||||||
fullscreen: process.env.VITE_DEV_SERVER_URL ? false : true,
|
|
||||||
simpleFullscreen: true,
|
|
||||||
frame: process.env.VITE_DEV_SERVER_URL ? true : false,
|
|
||||||
webPreferences: {
|
|
||||||
// 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法
|
|
||||||
nodeIntegration: true,
|
|
||||||
contextIsolation: false
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (process.env.VITE_DEV_SERVER_URL) {
|
|
||||||
win.loadURL(process.env.VITE_DEV_SERVER_URL);
|
|
||||||
} else {
|
|
||||||
win.loadFile(path.resolve(__dirname, "../dist/index.html"));
|
|
||||||
}
|
|
||||||
electron.app.on("activate", () => {
|
|
||||||
if (electron.BrowserWindow.getAllWindows().length === 0) {
|
|
||||||
createWindow();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
electron.ipcMain.on("quitHandler", (_, msg) => {
|
|
||||||
electron.app.quit();
|
|
||||||
});
|
|
||||||
electron.ipcMain.on("getPrintList", () => {
|
|
||||||
win.webContents.getPrintersAsync().then((res) => {
|
|
||||||
win.webContents.send("printList", res);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
const printWin = new electron.BrowserWindow({
|
|
||||||
show: false,
|
|
||||||
width: 464,
|
|
||||||
height: 1726,
|
|
||||||
webPreferences: {
|
|
||||||
// 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法
|
|
||||||
nodeIntegration: true,
|
|
||||||
contextIsolation: false
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (process.env.VITE_DEV_SERVER_URL) {
|
|
||||||
printWin.loadFile(path.join(__dirname, "../public/print.html"));
|
|
||||||
} else {
|
|
||||||
printWin.loadFile(path.resolve(__dirname, "../dist/print.html"));
|
|
||||||
}
|
|
||||||
electron.ipcMain.on("printerInfoSync", (event, arg) => {
|
|
||||||
printWin.webContents.send("getParams", arg);
|
|
||||||
});
|
|
||||||
electron.ipcMain.on("printStart", (event, arg) => {
|
|
||||||
console.log(arg);
|
|
||||||
const _parmas = JSON.parse(arg);
|
|
||||||
let name = _parmas.deviceName;
|
|
||||||
printWin.webContents.print({
|
|
||||||
silent: true,
|
|
||||||
deviceName: name,
|
|
||||||
pageSize: {
|
|
||||||
width: 58e3,
|
|
||||||
height: 216e3
|
|
||||||
},
|
|
||||||
scaleFactor: 80,
|
|
||||||
landscape: false,
|
|
||||||
margins: {
|
|
||||||
marginType: "none",
|
|
||||||
top: 0,
|
|
||||||
bottom: 0,
|
|
||||||
left: 0,
|
|
||||||
right: 0
|
|
||||||
},
|
|
||||||
dpi: {
|
|
||||||
horizontal: 203,
|
|
||||||
vertical: 203
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
const workPrintWin = new electron.BrowserWindow({
|
|
||||||
show: false,
|
|
||||||
width: 464,
|
|
||||||
height: 1726,
|
|
||||||
webPreferences: {
|
|
||||||
nodeIntegration: true,
|
|
||||||
contextIsolation: false
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (process.env.VITE_DEV_SERVER_URL) {
|
|
||||||
workPrintWin.loadFile(path.join(__dirname, "../public/work_print.html"));
|
|
||||||
} else {
|
|
||||||
workPrintWin.loadFile(path.resolve(__dirname, "../dist/work_print.html"));
|
|
||||||
}
|
|
||||||
electron.ipcMain.on("printerWorkSync", (event, arg) => {
|
|
||||||
workPrintWin.webContents.send("getParams", arg);
|
|
||||||
});
|
|
||||||
electron.ipcMain.on("printWorkStart", (event, arg) => {
|
|
||||||
const _parmas = JSON.parse(arg);
|
|
||||||
let name = _parmas.deviceName;
|
|
||||||
workPrintWin.webContents.print({
|
|
||||||
silent: true,
|
|
||||||
deviceName: name,
|
|
||||||
pageSize: {
|
|
||||||
width: 58e3,
|
|
||||||
height: 216e3
|
|
||||||
},
|
|
||||||
scaleFactor: 80,
|
|
||||||
landscape: false,
|
|
||||||
margins: {
|
|
||||||
marginType: "none",
|
|
||||||
top: 0,
|
|
||||||
bottom: 0,
|
|
||||||
left: 0,
|
|
||||||
right: 0
|
|
||||||
},
|
|
||||||
dpi: {
|
|
||||||
horizontal: 203,
|
|
||||||
vertical: 203
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
const tagPrintWin = new electron.BrowserWindow({
|
|
||||||
show: false,
|
|
||||||
width: 320,
|
|
||||||
height: 240,
|
|
||||||
webPreferences: {
|
|
||||||
nodeIntegration: true,
|
|
||||||
contextIsolation: false
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (process.env.VITE_DEV_SERVER_URL) {
|
|
||||||
tagPrintWin.loadFile(path.join(__dirname, "../public/tag_print.html"));
|
|
||||||
} else {
|
|
||||||
tagPrintWin.loadFile(path.resolve(__dirname, "../dist/tag_print.html"));
|
|
||||||
}
|
|
||||||
electron.ipcMain.on("printerTagSync", (event, arg) => {
|
|
||||||
console.log(arg);
|
|
||||||
tagPrintWin.webContents.send("getParams", arg);
|
|
||||||
});
|
|
||||||
electron.ipcMain.on("printTagStart", (event, arg) => {
|
|
||||||
const _parmas = JSON.parse(arg);
|
|
||||||
let name = _parmas.deviceName;
|
|
||||||
tagPrintWin.webContents.print({
|
|
||||||
silent: true,
|
|
||||||
deviceName: name,
|
|
||||||
pageSize: {
|
|
||||||
width: 4e4,
|
|
||||||
height: 3e4
|
|
||||||
},
|
|
||||||
scaleFactor: 80,
|
|
||||||
landscape: false,
|
|
||||||
margins: {
|
|
||||||
marginType: "none",
|
|
||||||
top: 0,
|
|
||||||
bottom: 0,
|
|
||||||
left: 0,
|
|
||||||
right: 0
|
|
||||||
},
|
|
||||||
dpi: {
|
|
||||||
horizontal: 203,
|
|
||||||
vertical: 203
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
electron.app.on("window-all-closed", () => {
|
|
||||||
if (process.platform !== "darwin")
|
|
||||||
electron.app.quit();
|
|
||||||
});
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "vite-electron",
|
"name": "vite-electron",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "1.3.41",
|
"version": "1.3.42",
|
||||||
"main": "dist-electron/main.js",
|
"main": "dist-electron/main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "chcp 65001 && vite",
|
"dev": "chcp 65001 && vite",
|
||||||
|
|
|
||||||
75
src/App.vue
75
src/App.vue
|
|
@ -2,7 +2,7 @@
|
||||||
<el-config-provider size="large">
|
<el-config-provider size="large">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="left" v-if="!hideLeftMenu">
|
<div class="left" v-if="!hideLeftMenu">
|
||||||
<left-menu ref="leftMenuRef" @connectWsHandle="initWebSocket()" />
|
<left-menu ref="leftMenuRef" @connectWsHandle="initWebSocket" />
|
||||||
</div>
|
</div>
|
||||||
<div :class="{ view: !hideLeftMenu }">
|
<div :class="{ view: !hideLeftMenu }">
|
||||||
<!-- <div class="wrapper">
|
<!-- <div class="wrapper">
|
||||||
|
|
@ -72,10 +72,10 @@ watch(route, (to) => {
|
||||||
}
|
}
|
||||||
if (to.fullPath == '/login') {
|
if (to.fullPath == '/login') {
|
||||||
if (ws.value != null) {
|
if (ws.value != null) {
|
||||||
console.log('关闭ws');
|
console.log('退出登录关闭ws');
|
||||||
ws.value.close()
|
ws.value.close()
|
||||||
ws.value = null
|
ws.value = null
|
||||||
wsIsClose.value = true
|
// wsIsClose.value = true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 打开ws
|
// 打开ws
|
||||||
|
|
@ -114,10 +114,12 @@ let wsIsClose = ref(false)
|
||||||
function initWebSocket(wsUrl = import.meta.env.VITE_API_WSS) {
|
function initWebSocket(wsUrl = import.meta.env.VITE_API_WSS) {
|
||||||
createUUID()
|
createUUID()
|
||||||
|
|
||||||
wsIsClose.value = false
|
// wsIsClose.value = false
|
||||||
ws.value = new ReconnectingWebSocket(wsUrl, null, {
|
if (ws.value == null) {
|
||||||
reconnectInterval: 10000
|
ws.value = new ReconnectingWebSocket(`${wsUrl}`, null, {
|
||||||
})
|
reconnectInterval: 10000
|
||||||
|
})
|
||||||
|
}
|
||||||
// console.log("websocket:", ws.value);
|
// console.log("websocket:", ws.value);
|
||||||
|
|
||||||
ws.value.addEventListener('open', function (event) {
|
ws.value.addEventListener('open', function (event) {
|
||||||
|
|
@ -130,9 +132,9 @@ function initWebSocket(wsUrl = import.meta.env.VITE_API_WSS) {
|
||||||
startheartbeat()
|
startheartbeat()
|
||||||
|
|
||||||
// 清除重连
|
// 清除重连
|
||||||
clearInterval(reConnectTimer.value)
|
// clearInterval(reConnectTimer.value)
|
||||||
reConnectTimer.value = null
|
// reConnectTimer.value = null
|
||||||
reConnectCount.value = 0
|
// reConnectCount.value = 0
|
||||||
|
|
||||||
ws.value.send(JSON.stringify({
|
ws.value.send(JSON.stringify({
|
||||||
type: "connect",
|
type: "connect",
|
||||||
|
|
@ -212,7 +214,8 @@ function initWebSocket(wsUrl = import.meta.env.VITE_API_WSS) {
|
||||||
heartbeatTimer.value = null
|
heartbeatTimer.value = null
|
||||||
|
|
||||||
// 手动关闭后不在执行自动连接任务
|
// 手动关闭后不在执行自动连接任务
|
||||||
if (!wsIsClose.value) reConnect(wsUrl);
|
// ws.value.close();
|
||||||
|
// if (!wsIsClose.value) reConnect(wsUrl);
|
||||||
})
|
})
|
||||||
|
|
||||||
// 连接发生错误
|
// 连接发生错误
|
||||||
|
|
@ -237,7 +240,8 @@ function initWebSocket(wsUrl = import.meta.env.VITE_API_WSS) {
|
||||||
heartbeatTimer.value = null
|
heartbeatTimer.value = null
|
||||||
|
|
||||||
// 手动关闭后不在执行自动连接任务
|
// 手动关闭后不在执行自动连接任务
|
||||||
if (!wsIsClose.value) reConnect(wsUrl);
|
// ws.value.close();
|
||||||
|
// if (!wsIsClose.value) reConnect(wsUrl);
|
||||||
})
|
})
|
||||||
|
|
||||||
// 关闭
|
// 关闭
|
||||||
|
|
@ -264,28 +268,31 @@ function startheartbeat() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重连最大次数5次
|
// 重连最大次数5次
|
||||||
let reConnectCount = ref(0)
|
// let reConnectCount = ref(0)
|
||||||
let reConnectTimer = ref(null)
|
// let reConnectTimer = ref(null)
|
||||||
function reConnect(wsUrl) {
|
// function reConnect(wsUrl) {
|
||||||
if (reConnectTimer.value != null) return
|
// if (reConnectTimer.value != null) return
|
||||||
ws.value.reconnect();
|
// console.log('ws.value.readyState', ws.value.readyState);
|
||||||
reConnectTimer.value = setInterval(() => {
|
// console.log('ReconnectingWebSocket.OPEN', ReconnectingWebSocket.OPEN);
|
||||||
// 自动连接超过5次不在连接,需手动出发
|
// if (ws.value.readyState == ReconnectingWebSocket.OPEN) return
|
||||||
// console.log('reConnectCount.value===', reConnectCount.value);
|
// ws.value.reconnect();
|
||||||
// if (reConnectCount.value >= 100) {
|
// reConnectTimer.value = setInterval(() => {
|
||||||
// console.log('重连超过5次,不在连接');
|
// // 自动连接超过5次不在连接,需手动出发
|
||||||
// clearInterval(reConnectTimer.value)
|
// // console.log('reConnectCount.value===', reConnectCount.value);
|
||||||
// reConnectTimer.value = null
|
// // if (reConnectCount.value >= 100) {
|
||||||
// reConnectCount.value = 0
|
// // console.log('重连超过5次,不在连接');
|
||||||
// wsIsClose.value = true
|
// // clearInterval(reConnectTimer.value)
|
||||||
// ws.value.close()
|
// // reConnectTimer.value = null
|
||||||
// } else {
|
// // reConnectCount.value = 0
|
||||||
// }
|
// // wsIsClose.value = true
|
||||||
// reConnectCount.value++
|
// // ws.value.close()
|
||||||
// initWebSocket(wsUrl)
|
// // } else {
|
||||||
ws.value.reconnect();
|
// // }
|
||||||
}, 2000)
|
// // reConnectCount.value++
|
||||||
}
|
// // initWebSocket(wsUrl)
|
||||||
|
// ws.value.reconnect();
|
||||||
|
// }, 2000)
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,28 @@
|
||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
|
import ReconnectingWebSocket from "reconnecting-websocket";
|
||||||
|
|
||||||
export const useSocket = defineStore({
|
export const useSocket = defineStore({
|
||||||
id: "socket",
|
id: "socket",
|
||||||
state: () => ({
|
state: () => ({
|
||||||
online: false,
|
online: false, // 是否在线
|
||||||
|
ws: null,
|
||||||
}),
|
}),
|
||||||
actions: {
|
actions: {
|
||||||
// 登录
|
// 改变在线状态
|
||||||
changeOnline(state) {
|
changeOnline(state) {
|
||||||
this.online = state;
|
this.online = state;
|
||||||
},
|
},
|
||||||
|
// 初始化
|
||||||
|
init(wsUrl = import.meta.env.VITE_API_WSS) {
|
||||||
|
if (this.ws == null) {
|
||||||
|
this.ws = new ReconnectingWebSocket(
|
||||||
|
`${wsUrl}/shopId=${store.userInfo.shopId}/clientId=${uuid.value}`,
|
||||||
|
null,
|
||||||
|
{
|
||||||
|
reconnectInterval: 10000,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -191,7 +191,9 @@ const exit = async () => {
|
||||||
let res = await loginlogout({
|
let res = await loginlogout({
|
||||||
status: 1
|
status: 1
|
||||||
})
|
})
|
||||||
useStorage.clear()
|
// useStorage.clear()
|
||||||
|
useStorage.del('userInfo')
|
||||||
|
useStorage.del('token')
|
||||||
ElMessage.success("交班成功");
|
ElMessage.success("交班成功");
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
router.replace({
|
router.replace({
|
||||||
|
|
@ -208,7 +210,9 @@ const exit = async () => {
|
||||||
data.printTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
|
data.printTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
|
||||||
ipcRenderer.send("printerWorkSync", JSON.stringify(data));
|
ipcRenderer.send("printerWorkSync", JSON.stringify(data));
|
||||||
// return
|
// return
|
||||||
useStorage.clear()
|
// useStorage.clear()
|
||||||
|
useStorage.del('userInfo')
|
||||||
|
useStorage.del('token')
|
||||||
ElMessage.success("交班成功");
|
ElMessage.success("交班成功");
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
router.replace({
|
router.replace({
|
||||||
|
|
@ -222,7 +226,9 @@ const exit = async () => {
|
||||||
let res = await loginlogout({
|
let res = await loginlogout({
|
||||||
status: 1
|
status: 1
|
||||||
})
|
})
|
||||||
useStorage.clear()
|
// useStorage.clear()
|
||||||
|
useStorage.del('userInfo')
|
||||||
|
useStorage.del('token')
|
||||||
ElMessage.success("交班成功");
|
ElMessage.success("交班成功");
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
router.replace({
|
router.replace({
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue