优化bug
This commit is contained in:
@@ -222,6 +222,7 @@ const defaultData = {
|
||||
const getDefaultState = () => {
|
||||
return {
|
||||
isConnect: false,
|
||||
connecting: false,
|
||||
serviceStatus: false,
|
||||
workStatus: true,
|
||||
reconnections: 0,
|
||||
@@ -250,9 +251,10 @@ const actions = {
|
||||
// 初始化websocket
|
||||
initWebSocket({ state, commit, dispatch, rootState }) {
|
||||
if (typeof WebSocket === 'undefined') return console.log('您的浏览器不支持websocket')
|
||||
if ((websock && state.isConnect) || !rootState.user.token) {
|
||||
if ((websock && state.isConnect) || !rootState.user.token || state.connecting) {
|
||||
return
|
||||
}
|
||||
commit('setData', { name: 'connecting', data: true })
|
||||
websock = new WebSocket(process.env.VUE_APP_SOCKET_API)
|
||||
websock.onmessage = function (res) {
|
||||
dispatch('websocketonmessage', res)
|
||||
@@ -399,10 +401,12 @@ const actions = {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!state.serviceStatus) {
|
||||
commit('setData', { name: 'printer_error', data: {type: 'warning', message: '服务连接中,请耐心等待', notify: '' } })
|
||||
reject()
|
||||
return
|
||||
}
|
||||
if (!state.isConnect) {
|
||||
commit('setData', { name: 'printer_error', data: {type: 'warning', message: '请先打开服务', notify: '' } })
|
||||
reject()
|
||||
return
|
||||
}
|
||||
websock.send(JSON.stringify(res))
|
||||
@@ -412,6 +416,7 @@ const actions = {
|
||||
// 链接建立之后执行的方法
|
||||
websocketonopen: ({ commit, dispatch }, res) => {
|
||||
console.log('链接建立之后执行send方法发送数据', res)
|
||||
commit('setData', { name: 'connecting', data: false })
|
||||
commit('setData', { name: 'isConnect', data: true })
|
||||
commit('setData', { name: 'serviceStatus', data: true })
|
||||
},
|
||||
@@ -419,6 +424,7 @@ const actions = {
|
||||
websocketonclose: ({ state, commit, dispatch }, res) => {
|
||||
console.log('断开链接', res)
|
||||
// 关闭
|
||||
commit('setData', { name: 'connecting', data: false })
|
||||
commit('setData', { name: 'isConnect', data: false })
|
||||
for (let key in defaultData) {
|
||||
commit('setData', { name: key, data: defaultData[key] })
|
||||
@@ -432,6 +438,7 @@ const actions = {
|
||||
},
|
||||
// 链接错误之后执行的方法
|
||||
websocketonerror({ state, commit, dispatch }, res) {
|
||||
commit('setData', { name: 'connecting', data: false })
|
||||
commit('setData', { name: 'isConnect', data: false })
|
||||
commit('setData', { name: 'serviceStatus', data: false })
|
||||
state.reconnections += 1
|
||||
|
||||
Reference in New Issue
Block a user