websocket优化关闭逻辑,补充登录请求websocket逻辑

main
zhangqun 2 years ago
parent 6722a780d6
commit 64166d6d37
  1. 55
      src/App.vue
  2. 4
      vue.config.js

@ -5,6 +5,7 @@
</template>
<script>
import { mapGetters } from "vuex";
export default {
name: "app",
data() {
@ -16,28 +17,49 @@ export default {
serverTimeoutObj: null, //
timeoutnum: null, //
lockReconnect: false, //
websocket: null
websocket: null,
isClose: false,//
};
},
watch: {},
computed: {
...mapGetters([
"token",
"userInfo"
])
},
watch: {
token: {
handler (newVal) {
console.log('token', newVal ? true : false)
if(newVal) {
this.isClose = false;
if(this.userInfo) {
this.initWebSocket(this.userInfo.user_id);
}
}else{
this.isClose = true;
this.closeWebSocket();
}
}
}
},
created() {
window.addEventListener('beforeunload', e => this.closeWebSocket(e));
},
mounted() {
this.initWebSocket('pc'); // userIdsocket
mounted() {
// this.initWebSocket('pc');
},
beforeDestroy() {
window.addEventListener('beforeunload', e => this.closeWebSocket(e));
},
methods: {
initWebSocket(supplierId) {
// WebSocketwshttpwsshttps
let wsUrl = `ws://171.16.8.51:81/websocket/${supplierId}`//`ws://192.168.3.32:81/websocket/${supplierId}`;
let wsUrl = `ws://192.168.3.32:81/websocket/${supplierId}`;//`ws://171.16.8.51:81/websocket/${supplierId}`//
this.websocket = new WebSocket(wsUrl);
this.websocket.onopen = this.websocketonopen;
this.websocket.onerror = this.websocketonerror;
this.websocket.onmessage = this.setOnmessageMessage;
this.websocket.onclose = this.websocketclose;
this.websocket.onclose = this.websocketClose();
},
start() {
// console.log('start');
@ -52,7 +74,7 @@ export default {
}
//serversocket
this.serverTimeoutObj = setTimeout(() => {
this.websocket.close();
this.websocketclose();
}, this.timeout)
}, this.timeout)
},
@ -70,7 +92,7 @@ export default {
//
this.timeoutnum && clearTimeout(this.timeoutnum);
this.timeoutnum = setTimeout(() => {
this.initWebSocket('pc');
this.initWebSocket(this.userInfo.user_id);
this.lockReconnect = false;
}, 5000)
},
@ -86,6 +108,9 @@ export default {
// //
// if (event.data === 'success' || event.data === 'heartBath') return
},
websocketClose() {
console.log("WebSocket连接关闭===============");
},
websocketonopen() {
//
this.start();
@ -95,20 +120,24 @@ export default {
console.log("WebSocket连接发生错误" + e);
},
websocketclose(e) {
this.websocket.close();
// this.websocket.close()
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
console.log("WebSocket连接关闭");
console.log("WebSocket连接关闭重连");
this.reconnect();//
},
websocketsend(messsage) {
this.websocket.send(messsage)
},
closeWebSocket() { // websocket
this.websocket.close()
if(this.websocket) {
console.log("WebSocket连接关闭");
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
this.websocket.close();
}
},
},
computed: {}
};
</script>
<style lang="scss">

@ -39,8 +39,8 @@ module.exports = {
proxy: {
"/api": {
//本地服务接口地址
target: 'http://192.168.1.34:81',
// target: 'http://192.168.3.32:81',
// target: 'http://192.168.1.34:81',
target: 'http://192.168.3.32:81',
// target: 'http://192.168.1.102:81',
ws: true,
pathRewrite: {

Loading…
Cancel
Save