<template></template>
<script>
export default {
//自己选择初始化位置
created () {
this.initWebSocket()
},
methods: {
//初始化连接
initWebSocket: function () {
//此处为服务端地址
let host = window.location.hostname
const url = `ws://${host}:9010/xxxxxx/websocket/123`
this.websock = new WebSocket(url)
this.websock.onopen = this.websocketonopen
this.websock.onerror = this.websocketonerror
this.websock.onmessage = this.websocketonmessage
},
websocketonopen: function () { },
websocketonerror: function (e) {
this.$message.error('网络连接失败!')
},
//接受服务端消息
websocketonmessage: function ({ data }) {
},
//关闭连接
websocketclose: function (e) { }
}
}
</script>
10-20
218
11-01
5683