1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| import { nanoid } from 'nanoid' import { isValuable } from '@/utils/util' export default { state: { websocket: null, lockReconnect: false, pingTimeout: 15000, pingTimeoutObj: null, serverTimeoutObj: null, reTimeoutNum: null, callbackFunc: {}, apiName: {}, timeObj: {} }, mutations: { webSocketInit(state) { state.websocket = new WebSocket('ws://127.0.0.1:3200') state.websocket.onopen = res => { console.log("Connection success...", res) this.commit('webSocketStart') } state.websocket.onmessage = res => { const { uniqueType, context } = JSON.parse(res.data) if (uniqueType === 'ping' && context === 'pong') { this.commit('webSocketReset') console.log("socket-pong") } else { console.log(state.apiName[uniqueType], `${(Date.now() - state.timeObj[uniqueType]) / 1000}秒`) delete state.apiName[uniqueType] delete state.timeObj[uniqueType] this.commit('webSocketCallApi', { uniqueType, context }) } } state.websocket.onclose = res => { console.log("Connection closed...", res) this.commit('webSocketReconnect') } state.websocket.onerror = res => { console.log("Connection error...", res) this.commit('webSocketReconnect') } }, webSocketSend(state, { apiUrl, context, callback = null}) { if (state.websocket && state.websocket.readyState === 1) { const uniqueType = nanoid() const c = isValuable(context) ? typeof context === 'string' ? context : JSON.stringify(context) : null const data = JSON.stringify( { uniqueType, apiUrl, context: c } ) console.log('query data', data) state.apiName[uniqueType] = apiUrl state.timeObj[uniqueType] = Date.now() state.websocket.send(data) if (callback) state.callbackFunc[uniqueType] = callback } }, webSocketCallApi (state, { uniqueType, context }) { if (uniqueType === 'ping') return const res = JSON.parse(context) if (state.callbackFunc[uniqueType]) { state.callbackFunc[uniqueType](res) this.commit('setWsCallBackFunc', { uniqueType, callback: null }) } }, setWsCallBackFunc (state, data) { if (data.callback === null) { delete state.callbackFunc[data.uniqueType] } else { state.callbackFunc[data.uniqueType] = data.callback } }, webSocketReconnect(state) { if (state.lockReconnect) { return } state.lockReconnect = true state.reTimeoutNum && clearTimeout(state.reTimeoutNum) state.reTimeoutNum = setTimeout(() => { this.commit('webSocketInit') state.lockReconnect = false }, 5000) }, webSocketReset(state) { clearTimeout(state.pingTimeoutObj) clearTimeout(state.serverTimeoutObj) this.commit('webSocketStart') }, webSocketStart(state) { state.pingTimeoutObj && clearTimeout(state.pingTimeoutObj) state.serverTimeoutObj && clearTimeout(state.serverTimeoutObj) state.pingTimeoutObj = setTimeout(() => { if (state.websocket.readyState === 1) { const ping = JSON.stringify({ uniqueType: 'ping', context: 'ping' }) state.websocket.send(ping) } else { this.commit('webSocketReconnect') } this.serverTimeoutObj = setTimeout(() => { state.websocket.close() }, 60000) }, state.pingTimeout) }, }, actions: { webSocketSend({ commit }, obj, func) { commit('webSocketSend', obj, func) } } }
|