wsUrl: 'ws://1xxxxxxx:8099/imserver/9999', // ws地址
websock: null, // ws实例
// 初始化weosocket
initWebSocket() {
if (typeof WebSocket === 'undefined')
return console.log('您的浏览器不支持websocket')
this.websock = new WebSocket(this.wsUrl)
this.websock.onmessage = this.websocketonmessage
this.websock.onopen = this.websocketonopen
this.websock.onerror = this.websocketonerror
this.websock.onclose = this.websocketclose
},
websocketonopen() {
// 连接建立之后执行send方法发送数据
let actions = { test: 'test' }
this.websocketsend(JSON.stringify(actions))
},
websocketonerror() {
// 连接建立失败重连
this.initWebSocket()
},
websocketonmessage(e) {
console.log(e)
// 数据接收
const redata = JSON.parse(e.data)
if (redata.code == 200) {
this.fullscreenLoading = false
this.rLoading.close();
this.$message({
message: 'xxxxxxxxxxxxxx!',
type: 'success'
});
this.getTableData()
} else {
// this.initWebSocket()
}
console.log('接收的数据', redata)
},
websocketsend(Data) {
// 数据发送
this.websock.send(Data)
},
websocketclose(e) {
// 关闭
console.log('断开连接', e)
},
|