initWebSocket () {
const wsuri = (process.env.VUE_APP_BASE_API).replace("https", "ws").replace("http", "ws") + '/webSocket/' + (this.$store.getters && this.$store.getters.name)
console.log(wsuri)
this.websock = new WebSocket(wsuri)
this.websock.onopen = this.websocketOnopen
this.websock.onerror = this.websocketOnerror
this.websock.onmessage = this.websocketOnmessage
this.websock.onclose = this.websocketOnclose
},
// 客户端和服务端建立链接时触发,此时可向服务端传递参数
websocketOnopen () {
// this.websock.send('crew'); //传递参数
},
// 客户端收到服务端发来的消息时,会触发onmessage事件,参数res.data中包含server传输过来的数据
websocketOnmessage (res) {
//接收到消息的回调方法,我这里是处理返回数据
}
//如果出现连接,处理,接收,发送数据失败的时候就会触发onerror事件
websocketOnerror () {
console.log('连接失败')
},
//客户端收到服务端发送的关闭连接的请求时,触发onclose事件
websocketOnclose () {
this.websock.close()
console.log('关闭连接')
},
|