fun_websocket() {
this.loadingImg = true
// 初始化weosocket
const wsuriImg = 'ws://' + window.ipConfig.IMG_RECOGNITION_URL
this.websockImg = new WebSocket(wsuriImg)
this.websockImg.onopen = this.websocketonopenImg
this.websockImg.onerror = this.websocketonerrorImg
this.websockImg.onmessage = this.websocketonmessageImg
this.websockImg.onclose = this.websocketcloseImg
},
// 连接建立之后执行send方法发送数据
websocketonopenImg() {
const obj = {}
// 数据发送
this.websockImg.send(JSON.stringify(obj))
},
// 连接建立失败重连
websocketonerrorImg() {
this.loading = false
console.log('连接建立失败重连')
// this.fun_websocket()
},
// 数据接收
websocketonmessageImg(e) {
this.loading = false
console.log(e.data)
},
// 关闭
websocketcloseImg() {
this.websockImg.close()
console.log('关闭---')
},
前端websocket使用
于 2024-03-28 11:25:34 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)