验证中...
Languages: HTML
Categories: JavaScript 工具
Latest update 2019-09-07 09:13
1.在request.js中添加如下代码,我的后台程序还需要一个opname参数,所以我定义了一个opname
Raw Copy
export const wsuri = `ws://127.0.0.1:61101//WebMoudule//websocket`
export const opname = `OP0002`
在vue页中impot入wsuri和opname
Raw Copy
import { wsuri, opname } from '@/utils/request'
3.连接socket并发送opname
Raw Copy
initWebsocket() { // 初始化websocket
this.websock = new WebSocket(wsuri)// 这里面的this都指向vue
this.websock.onopen = this.websocketopen
this.websock.onmessage = this.websocketonmessage
this.websock.onclose = this.websocketclose
this.websock.onerror = this.websocketerror
},
websocketopen() { // 打开
console.log('WebSocket连接成功')
this.websock.send('{<' + opname + '>}')
},
websocketonmessage(msg) { // 数据接收
console.log(msg)
this.msg = msg
},
websocketclose() { // 关闭
console.log('WebSocket关闭')
},
websocketerror() { // 失败
console.log('WebSocket连接失败')
},
钩子函数,连接
Raw Copy
created(){
this.initWebsocket()
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search