/**
* 创建websocket
*/
initWebSocket(){
console.log("创建WebSocket")
this.websocket = new WebSocket("ws://192.168.0.113:7667/Socket/pushMessage")
this.websocket.onmessage = this.websocketonmessage
this.websocket.onerror = this.websocketonerror
this.websocket.onopen = this.websocketonopen
this.websocket.onclose = this.websocketclose
},
// 连接建立之后执行send方法发送数据
websocketonopen () {
let data = {
code: 0,
msg: '这是client:初次连接'
}
this.websocketsend(data)
},
websocketonerror () {
console.log( 'WebSocket连接失败')
},
// 数据接收
websocketonmessage (e) {
console.log('数据接收' + e.data)
},
// 数据发送
websocketsend (Data) {
this.websocket.send(Data)
},
// 关闭
websocketclose (e) {
console.log('已关闭连接', e)
}
码一下,没有测试过