优化wss连接

This commit is contained in:
gyq 2024-07-04 10:04:01 +08:00
parent 78672a0cf9
commit c7758f66d7
7 changed files with 162 additions and 44 deletions

View File

@ -8,8 +8,9 @@ VITE_API_WSS = 'wss://cashier.sxczgkj.cn/client'
#测试ws #测试ws
# VITE_API_WSS = 'wss://wxcashiertest.sxczgkj.cn/client' # VITE_API_WSS = 'wss://wxcashiertest.sxczgkj.cn/client'
# 测试 # 阿伟本地ws
# VITE_API_URL = 'https://cashier-client.sxczgkj.cn/cashier-client' # VITE_API_WSS = 'ws://192.168.2.17:9998/client'
# 测试 php # 测试 php
VITE_API_PHP_URL = 'http://192.168.2.33:1666/index.php/api' VITE_API_PHP_URL = 'http://192.168.2.33:1666/index.php/api'
@ -20,5 +21,8 @@ VITE_API_PHP_URL = 'http://192.168.2.33:1666/index.php/api'
# 鹏辉 # 鹏辉
# VITE_API_URL = 'http://192.168.2.41:10589/cashier-client' # VITE_API_URL = 'http://192.168.2.41:10589/cashier-client'
# 测试
# VITE_API_URL = 'https://cashier-client.sxczgkj.cn/cashier-client'
# 正式 # 正式
VITE_API_URL = 'https://cashierclient.sxczgkj.cn/cashier-client' VITE_API_URL = 'https://cashierclient.sxczgkj.cn/cashier-client'

View File

@ -2,16 +2,16 @@
ENV = production ENV = production
# 正式ws # 正式ws
# VITE_API_WSS = 'wss://cashier.sxczgkj.cn/client' VITE_API_WSS = 'wss://cashier.sxczgkj.cn/client'
#测试ws #测试ws
VITE_API_WSS = 'wss://wxcashiertest.sxczgkj.cn/client' # VITE_API_WSS = 'wss://wxcashiertest.sxczgkj.cn/client'
# 线上环境接口地址
VITE_API_URL = 'https://cashierclient.sxczgkj.cn/cashier-client/'
# 测试 php # 测试 php
VITE_API_PHP_URL = 'http://192.168.2.33:1666/index.php/api' VITE_API_PHP_URL = 'http://192.168.2.33:1666/index.php/api'
# 测试 # 测试
# VITE_API_URL = 'https://cashier-client.sxczgkj.cn/cashier-client' # VITE_API_URL = 'https://cashier-client.sxczgkj.cn/cashier-client'
# 线上环境接口地址
VITE_API_URL = 'https://cashierclient.sxczgkj.cn/cashier-client/'

View File

@ -1,7 +1,7 @@
{ {
"name": "vite-electron", "name": "vite-electron",
"private": true, "private": true,
"version": "1.3.26", "version": "1.3.36",
"main": "dist-electron/main.js", "main": "dist-electron/main.js",
"scripts": { "scripts": {
"dev": "chcp 65001 && vite", "dev": "chcp 65001 && vite",
@ -19,6 +19,7 @@
"lodash": "^4.17.21", "lodash": "^4.17.21",
"pinia": "^2.1.7", "pinia": "^2.1.7",
"qrcode": "^1.5.3", "qrcode": "^1.5.3",
"reconnecting-websocket": "^4.4.0",
"serialport": "^12.0.0", "serialport": "^12.0.0",
"swiper": "^11.1.1", "swiper": "^11.1.1",
"uuid": "^10.0.0", "uuid": "^10.0.0",

View File

@ -33,6 +33,7 @@ import { scanSendMessage } from '@/api/order/index'
import { useGlobal } from '@/store/global.js' import { useGlobal } from '@/store/global.js'
import { useSocket } from '@/store/socket.js' import { useSocket } from '@/store/socket.js'
import { usePrint } from '@/store/print.js' import { usePrint } from '@/store/print.js'
import ReconnectingWebSocket from 'reconnecting-websocket';
const global = useGlobal() const global = useGlobal()
const socketStore = useSocket() const socketStore = useSocket()
@ -113,10 +114,12 @@ function initWebSocket(wsUrl = import.meta.env.VITE_API_WSS) {
createUUID() createUUID()
wsIsClose.value = false wsIsClose.value = false
ws.value = new WebSocket(wsUrl); ws.value = new ReconnectingWebSocket(wsUrl, null, {
reconnectInterval: 10000
})
// console.log("websocket:", ws.value); // console.log("websocket:", ws.value);
ws.value.onopen = function () { ws.value.addEventListener('open', function (event) {
console.log('wss连接成功'); console.log('wss连接成功');
socketStore.changeOnline(true) socketStore.changeOnline(true)
@ -135,14 +138,37 @@ function initWebSocket(wsUrl = import.meta.env.VITE_API_WSS) {
shopId: store.userInfo.shopId, shopId: store.userInfo.shopId,
clientId: uuid.value clientId: uuid.value
})) }))
}; })
// // ws.value.onopen = function () {
ws.value.onmessage = function (e) { // console.log('wss');
// websocketonmessage(e); // socketStore.changeOnline(true)
// //
// clearInterval(heartbeatTimer.value)
// heartbeatTimer.value = null
// startheartbeat()
// //
// clearInterval(reConnectTimer.value)
// reConnectTimer.value = null
// reConnectCount.value = 0
// ws.value.send(JSON.stringify({
// type: "connect",
// shopId: store.userInfo.shopId,
// clientId: uuid.value
// }))
// };
ws.value.addEventListener('message', function (e) {
let data = JSON.parse(e.data) let data = JSON.parse(e.data)
if (data.type == 'order') { if (data.type == 'order') {
console.log('接收消息', data); console.log('接收消息', data);
ws.value.send(JSON.stringify({
type: "send",
orderNo: data.orderInfo.orderNo
}))
// //
// printBill(data) // printBill(data)
@ -150,10 +176,33 @@ function initWebSocket(wsUrl = import.meta.env.VITE_API_WSS) {
// checkLabelPrint(data) // checkLabelPrint(data)
printStore.labelPrint(data) printStore.labelPrint(data)
} }
}; })
// //
ws.value.onerror = function () { ws.value.addEventListener('WebSocket.CLOSED', function () {
})
//
// ws.value.onmessage = function (e) {
// // websocketonmessage(e);
// let data = JSON.parse(e.data)
// if (data.type == 'order') {
// console.log('', data);
// ws.value.send(JSON.stringify({
// type: "send",
// orderNo: data.orderInfo.orderNo
// }))
// //
// // printBill(data)
// //
// // checkLabelPrint(data)
// printStore.labelPrint(data)
// }
// };
ws.value.addEventListener('error', function () {
console.log("WebSocket连接发生错误"); console.log("WebSocket连接发生错误");
socketStore.changeOnline(false) socketStore.changeOnline(false)
@ -163,10 +212,22 @@ function initWebSocket(wsUrl = import.meta.env.VITE_API_WSS) {
// //
if (!wsIsClose.value) reConnect(wsUrl); if (!wsIsClose.value) reConnect(wsUrl);
}; })
// //
ws.value.onclose = function (e) { // ws.value.onerror = function () {
// console.log("WebSocket");
// socketStore.changeOnline(false)
// //
// clearInterval(heartbeatTimer.value)
// heartbeatTimer.value = null
// //
// if (!wsIsClose.value) reConnect(wsUrl);
// };
ws.value.addEventListener('error', function (e) {
console.log('ws关闭了', e); console.log('ws关闭了', e);
socketStore.changeOnline(false) socketStore.changeOnline(false)
@ -176,13 +237,27 @@ function initWebSocket(wsUrl = import.meta.env.VITE_API_WSS) {
// //
if (!wsIsClose.value) reConnect(wsUrl); if (!wsIsClose.value) reConnect(wsUrl);
}; })
//
// ws.value.onclose = function (e) {
// console.log('ws', e);
// socketStore.changeOnline(false)
// //
// clearInterval(heartbeatTimer.value)
// heartbeatTimer.value = null
// //
// if (!wsIsClose.value) reConnect(wsUrl);
// };
} }
// //
let heartbeatTimer = ref(null) let heartbeatTimer = ref(null)
function startheartbeat() { function startheartbeat() {
heartbeatTimer.value = setInterval(() => { heartbeatTimer.value = setInterval(() => {
console.log('发送心跳');
ws.value.send(JSON.stringify({ type: 'heartbeat' })) ws.value.send(JSON.stringify({ type: 'heartbeat' }))
}, 10000) }, 10000)
} }
@ -192,6 +267,7 @@ 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();
reConnectTimer.value = setInterval(() => { reConnectTimer.value = setInterval(() => {
// 5 // 5
// console.log('reConnectCount.value===', reConnectCount.value); // console.log('reConnectCount.value===', reConnectCount.value);
@ -204,11 +280,41 @@ function reConnect(wsUrl) {
// ws.value.close() // ws.value.close()
// } else { // } else {
// } // }
reConnectCount.value++ // reConnectCount.value++
initWebSocket(wsUrl) // initWebSocket(wsUrl)
}, 5000) ws.value.reconnect();
}, 1000)
} }
//
function updateInfo() {
//
let isOnLine = navigator.onLine
//
let info = navigator.connection
console.log('isOnLine===', isOnLine);
console.log('info===', info);
if (isOnLine) {
console.log('有网了重连ws连接');
reConnect()
} else {
console.log('没网了断开ws连接');
ws.value.close()
//
clearInterval(reConnectTimer.value)
reConnectTimer.value = null
clearInterval(heartbeatTimer.value)
heartbeatTimer.value = null
}
}
const nextCodeRef = ref('') const nextCodeRef = ref('')
const lastTimeRef = ref('') const lastTimeRef = ref('')
const codeRef = ref('') const codeRef = ref('')
@ -304,6 +410,12 @@ onMounted(() => {
document.addEventListener('keydown', (e) => { document.addEventListener('keydown', (e) => {
getBarCode(e) getBarCode(e)
}) })
// 线
window.addEventListener("onLine", updateInfo)
// 线
window.addEventListener("offLine", updateInfo)
//
navigator.connection.addEventListener('change', updateInfo)
}) })
</script> </script>

View File

@ -1,23 +1,24 @@
import { defineStore } from "pinia" import { defineStore } from "pinia";
import { login } from '@/api/user' import { login } from "@/api/user";
import useStorage from '@/utils/useStorage' import useStorage from "@/utils/useStorage";
export const useUser = defineStore({ export const useUser = defineStore({
id: "user", id: "user",
state: () => ({ state: () => ({
userInfo: useStorage.get('userInfo'), userInfo: useStorage.get("userInfo"),
token: useStorage.get('token'), token: useStorage.get("token"),
}), }),
actions: { actions: {
// 登录 // 登录
userlogin(param) { userlogin(param) {
return login(param).then(res => { return login(param).then((res) => {
this.userInfo = res this.userInfo = res;
this.token = res.token this.token = res.token;
useStorage.set("token", this.token); useStorage.set("token", this.token);
// this.userInfo.shopId = "24";
useStorage.set("userInfo", this.userInfo); useStorage.set("userInfo", this.userInfo);
return this.userInfo; return this.userInfo;
}) });
} },
} },
}) });

View File

@ -132,7 +132,7 @@ const refundDialogRef = ref(null)
const tableData = reactive({ const tableData = reactive({
resetLoading: false, resetLoading: false,
proName: '', proName: '',
type: 2, type: 1,
status: '', status: '',
loading: false, loading: false,
list: [], list: [],

View File

@ -109,12 +109,12 @@ const submitHandle = () => {
if (valid) { if (valid) {
loading.value = true; loading.value = true;
store.userlogin(form).then(async (res) => { store.userlogin(form).then(async (res) => {
const douyin = await douyincheckIn({ // const douyin = await douyincheckIn({
token: res.token, // token: res.token,
loginName: res.loginName, // loginName: res.loginName,
clientType: 'pc' // clientType: 'pc'
}) // })
useStorage.set('douyin', douyin.userInfo) // useStorage.set('douyin', douyin.userInfo)
ElMessage.success("登录成功"); ElMessage.success("登录成功");
setTimeout(() => { setTimeout(() => {
router.replace({ router.replace({