created() {
this.initWebSocket();
},
methods: {
initWebSocket() {
const path = 'ws://192.168.107.10:8083/ws/zhangsan';
this.webSocket = new WebSocket(path);
this.webSocket.onmessage = this.webSocketMsg;
this.webSocket.onopen = this.openWebsocket;
this.webSocket.onerror = this.webSocketError;
this.webSocket.onclose = this.closeWebSocket;
},
openWebsocket() {
let heartMsg = {
messagetype: 7,
data: {
operation_type: 1,
operation_options: 0,
operation_data: null
},
err: ""
}
this.sendWebSocket(heartMsg);
},
webSocketError() {
this.initWebSocket();
},
webSocketMsg(e) {
console.log(e.data);
},
sendWebSocket(data) {
this.webSocket.send(JSON.stringify(data));
},
closeWebSocket(e) {
this.webSocket.close();
}
}