9 changed files with 136 additions and 8 deletions
-
2src/components/LoginForm.vue
-
10src/components/Operator.vue
-
15src/components/Setting/components/User.vue
-
5src/mock/command.js
-
5src/pages/Home.vue
-
4src/pages/Login.vue
-
62src/socket/index.js
-
3src/store/index.js
-
32src/store/modules/websocket.js
@ -0,0 +1,5 @@ |
|||||
|
export const startDisinfectionJSON = { |
||||
|
command: '1231', |
||||
|
roomSize: 12, |
||||
|
log: 6, |
||||
|
} |
@ -0,0 +1,62 @@ |
|||||
|
export default class Socket { |
||||
|
constructor(url, protocols) { |
||||
|
this.url = url |
||||
|
this.protocols = protocols |
||||
|
this.ws = null |
||||
|
this.reconnectTimeout = 1000 |
||||
|
this.maxReconnectTimes = 5 |
||||
|
} |
||||
|
|
||||
|
connect() { |
||||
|
this.ws = new WebSocket(this.url, this.protocols) |
||||
|
this.ws.onopen = () => { |
||||
|
console.log('WebSocket连接成功') |
||||
|
this.reconnectTimes = 0 |
||||
|
} |
||||
|
this.ws.onclose = () => { |
||||
|
console.log('WebSocket断开连接') |
||||
|
this.reconnect() |
||||
|
} |
||||
|
this.ws.onerror = err => { |
||||
|
console.log('WebSocket连接出错', err) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
reconnect() { |
||||
|
if (this.reconnectTimes < this.maxReconnectTimes) { |
||||
|
setTimeout(() => { |
||||
|
this.connect() |
||||
|
this.reconnectTimes++ |
||||
|
}, this.reconnectTimeout) |
||||
|
this.reconnectTimeout *= 2 |
||||
|
} else { |
||||
|
console.log('WebSocket重连超过最大次数,放弃重连') |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 消息发送
|
||||
|
msg(param) { |
||||
|
if (param === 'heartbeat') { |
||||
|
this.ws.send(param) |
||||
|
} else { |
||||
|
this.ws.send(JSON.stringify(param)) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 延迟发送
|
||||
|
timeout(param) { |
||||
|
setTimeout(() => { |
||||
|
this.msg(param) |
||||
|
}, 2000) |
||||
|
} |
||||
|
|
||||
|
send(param) { |
||||
|
if (this.ws.readyState === this.ws.OPEN) { |
||||
|
this.msg(param) |
||||
|
} else if (this.ws.readyState === this.ws.CONNECTING) { |
||||
|
this.timeout(param) |
||||
|
} else { |
||||
|
this.timeout(param) |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,32 @@ |
|||||
|
import { defineStore } from 'pinia' |
||||
|
import Socket from '@/socket' |
||||
|
export const useWebSocketStore = defineStore({ |
||||
|
id: 'websocket', // id必填,且需要唯一
|
||||
|
// state
|
||||
|
state: () => { |
||||
|
return { |
||||
|
socketInstance: null, |
||||
|
} |
||||
|
}, |
||||
|
// actions
|
||||
|
actions: { |
||||
|
initSocket() { |
||||
|
const url = 'ws://localhost:8899/websocket/nuclear' |
||||
|
const init = new Socket(url) |
||||
|
init.connect() |
||||
|
init.ws.onmessage = function (ev) { |
||||
|
console.log(ev) |
||||
|
// switch (data.type) {
|
||||
|
// case 1:
|
||||
|
// break
|
||||
|
// case 2:
|
||||
|
// break
|
||||
|
// }
|
||||
|
} |
||||
|
this.socketInstance = init |
||||
|
}, |
||||
|
sendMsg(message) { |
||||
|
this.socketInstance?.msg(message) |
||||
|
}, |
||||
|
}, |
||||
|
}) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue