其他分享
首页 > 其他分享> > vue websocket + Stomp数据连接

vue websocket + Stomp数据连接

作者:互联网

使用websocket页面引入

import SockJS from 'sockjs-client' import Stomp from 'stompjs'   订阅方法 webSocket(topic1,topic2,url) {   //topic为websocket的topic,url为接口地址       let that = this;       var domain = process.env.VUE_APP_BASE_API;       const url = domain+url       const socket = new SockJS(url)       const headers = {         Authorization: ''       }       this.stompClient = Stomp.over(socket)       this.stompClient.connect(headers, () => {     //订阅         that.stompClient.subscribe(topic1, (msg) => { //            const resdata = JSON.parse(msg.body)           console.log(resdata)         }, headers)     //发送     this.stompClient.send(topic2,{aaa:333},"Hellow World")   // {aaa:333} 为传递的header内容,‘Hellow World’为传递的body内容       },(err) => {       // 连接发生错误时的处理函数       console.log(err)       })     },   //断开连接 closeWebsocket(){        if(this.stompClient){         this.stompClient.disconnect()       }     },

标签:vue,websocket,url,stompClient,Stomp,headers,const
来源: https://www.cnblogs.com/wwxdxgb/p/16261720.html