vue中使用Sockjs和stompjs实现实时数据

引入

import SockJS from  'sockjs-client';
import  Stomp from 'stompjs';

代码

	  let socket = new SockJS('api/gcIot-supervise-manholecover/webSocketEndPoint');  // 路径
      this.stompClient = Stomp.over(socket);
      let headers = {
        Authorization:getToken()   // 获取token
      }
      this.stompClient.connect(headers,  (frame) => {   // 连接
          console.log(frame);
          this.stompClient.subscribe('/topic/greetings', (greeting) => {   // 这里接受后端推送的数据
            console.log(greeting)
          },headers);
          this.stompClient.send("/app/demo", headers,'134567');  // 发送数据 1234567
      },(err) => {
        console.log('失败')
        console.log(err)
      })

你可能感兴趣的:(实时数据)