export default {
data(){
return {}
},
created(){
this.initWebSocket();
},
destroyed(){
this.websock.close()
},
methods:{
initWebSocket() {
const wsuri = "ws://172.16.26.133:8080/xcdpWK/";
this.websock = new WebSocket(wsuri);
this.websock.onmessage = this.websocketonmessage;
this.websock.onopen = this.websocketonopen;
this.websock.onerror = this.websocketonerror;
this.websock.onclose = this.websocketclose;
console.log('weosocket已连接')
},
websocketonopen() {
let actions = {
"test": "12345"
};
this.websocketsend(JSON.stringify(actions));
console.log('消息发送成功!')
},
websocketonerror() {
console.log('weosocket连接建立失败')
this.initWebSocket();
},
websocketonmessage(e) {
console.log('weosocket数据接收')
const redata = JSON.parse(e.data);
console.log(redata)
},
websocketsend(Data) {
this.websock.send(Data);
},
websocketclose(e) {
console.log('断开连接', e);
},
}
}