前端vue使用stomp.js、sock.js完成websocket

摘要:
实例<button@click=“发送('2')”>10分钟<button@click=“发送('4')”>24小时<key=“索引”>{{item.value}}<//additionalheader'client-id':exportdefault{data(){return{socketUrl:
前端vue使用stomp.js、sock.js完成websocket

Sock.js

Sock.js 是一个JavaScript库,为了应对很多浏览器不支持websocket协议问题。SockJ会自动对应websocket,如果websocket不可用,就会自动降为轮训的方式。

Stomp.js

STOMP-Simple Text Oriented Message Protocol-面向消息的简单文本协议。Sockjs为websocket提供了备选方案,但是通信形式层级过低。Stompjs则增加了语义

WebSocket、SockJs、STOMP三者关系

WebSocket 是底层协议,SockJS 是WebSocket 的备选方案,也是底层协议,而 STOMP 是基于 WebSocket(SockJS)的上层协议。

  1. HTTP协议解决了 web 浏览器发起请求以及 web 服务器响应请求的细节,假设 HTTP 协议 并不存在,只能使用 TCP 套接字来 编写 web 应用。

  2. 直接使用 WebSocket(SockJS) 就很类似于 使用 TCP 套接字来编写 web 应用,因为没有高层协议,就需要我们定义应用间所发送消息的语义,还需要确保连接的两端都能遵循这些语义;

  3. 同HTTP在TCP 套接字上添加请求-响应模型层一样,STOMP在WebSocket 之上提供了一个基于帧的线路格式层,用来定义消息语义;

实例

<template>
  <div>
    <p>
      <button @click="send('1')">1分钟</button>
      <button @click="send('2')">10分钟</button>
      <button @click="send('3')">1小时</button>
      <button @click="send('4')">24小时</button>
    </p>
    <ul>
      <li v-for="(item,index) in tableData" :key="index">{{ item.name }}:{{ item.value }}</li>
    </ul>
  </div>
</template>

<script>
import Stomp from 'stompjs'
import SockJS from 'sockjs-client'

// const headers = {}
const headers = {
  login: 'mylogin',
  passcode: 'mypasscode',
  // additional header
  'client-id': 'my-client-id',
  Author: '1235465tyrgw32rsgg'
};
export default {
  data () {
    return {
      socketUrl: '/websocket/ws', // ws://localhost:8080/websocket/ws
      tableData: [],
      reconnecting: false,
      socket: null,
      stompClient: null
    }
  },
  mounted () {
    this.initWebsocket()
  },
  beforeDestroy() {
    this.closeSocket()
  },
  methods: {
    /* 只需要在连接服务器注册端点endPoint时,写访问服务器的全路径URL:
      new SockJS('http://127.0.0.1:9091/sbjm-cheng/endpointOyzc');
      其他监听指定服务器广播的URL不需要写全路径
      stompClient.subscribe('/topic/getResponse',function(response){})
    */
    /* 创建stompClient: (1) 使用原生的websocket (2) 使用定制的websocket(例如sockjs包裹的websocket)
    ① Stomp.client("ws://localhost:61614/stomp")
    ② SockJS的url是http、https协议,而不是 ws
      const socket = new SockJS("http://127.0.0.1:9091/sbjm-cheng/endpointOyzc"); 
       Stomp.over(socket)

       const ws = new Websocket("ws://localhost:61614/ws")
       Stomp.over(ws)
    */
    initWebsocket () {
      /* 
      ① 创建sockJS对象;
      ② 创建stomp客户端
      ③ stompClient客户端 连接 stomp服务器
      */
      this.socket = new SockJS(this.socketUrl)
      this.stompClient = Stomp.over(this.socket)

      this.stompClient.connect(
        headers, // headers头部信息。可添加客户端的认证信息。也可以不添加信息,headers 直接就设置为 {}
        frame => {
          // 连接成功: 订阅服务器的地址。为了浏览器可以接收到消息,必须先订阅服务器的地址
          this.connectSucceed()
        }, err => {
          // 连接失败的回调
          this.reconnect(this.socketUrl, this.connectSucceed)
        })
    },
    /* 连接成功的回调:订阅服务器的地址。为了浏览器可以接收到消息,必须先订阅服务器的地址 */
    connectSucceed () {
      // 设置心跳发送接受频率(ms)默认为10000ms。 heart-beating是利用window.setInterval()去规律地发送heart-beats或者检查服务端的heart-beats。
      this.stompClient.heartbeat.outgoing = 10000
      this.stompClient.heartbeat.incoming = 0

      this.stompClient.subscribe('/topic/dashboard/data', res => {
        this.tableData = res.body.list
      })

    /* 
    当客户端与服务端连接成功后,可以调用 send()来发送STOMP消息。这个方法必须有一个参数,用来描述对应的STOMP的目的地。
    另外可以有两个可选的参数:headers,object类型包含额外的信息头部;body,一个String类型的参数。

    client.send("/queue/test", {priority: 9}, "Hello, STOMP");
    // client会发送一个STOMP发送帧给/queue/test,这个帧包含一个设置了priority为9的header和内容为“Hello, STOMP”的body。
    */
      this.stompClient.send('/topic/dashboard/send',{}, '1')
    },
    reconnect (socketUrl, callback) {
      this.reconnecting = true
      let connected = false
      const timer = setInterval(() => {
        this.socket = new SockJS(socketUrl)
        this.stompClient = Stomp.over(this.socket)
        this.stompClient.connect(headers, frame => {
          this.reconnectting = false
          connected = true
          clearInterval(timer)
          callback()
        }, err => {
          console.log('Reconnect failed!');
          if(!connected) console.log(err);
        })
      }, 1000);
    },
    closeSocket(){
      if(this.stompClient != null){
        this.stompClient.disconnect()
        // this.stompClient.disconnect(()=>{
        //   console.log('连接关闭')
        // });
      }
    },
    send(flag){
      this.stompCLient.send('/topic/dashboard/send',{}, flag)
    }
  },
}
</script>

免责声明:文章转载自《前端vue使用stomp.js、sock.js完成websocket》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Docker 介绍安装Git入门常用命令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

mysql中文、英文别名排序问题,order by 关键字详解

 order by 关键字详解:   SELECT intcode AS 商品编码, product_title AS 名称, retailprice AS 零售价, purchaseprice AS 合同进价 FROM product_detail WHERE brand='0063飘柔' AND purchaseprice>10 ORDE...

AcWing 476. 对称二叉树

树哈希解法 令 f[x][0] 为 : x 的子树都严格按照先左后右的顺序 Hash 的值。 f[x][1] 先右后左。 判断 x 是不是对称的二叉树只要判断 f[lc[x]][0] 和 f[rc[x]][1] 是否相等。 递推式: f[x][0]=p1*s1*P+a[x]*NP+((~rc[x]) ? f[rc[x]][0] : 997)*NPC...

使用Web存储API存取本地数据

使用Web存储API TODO:本文由 赤石俊哉 翻译整理,您可以将本文自由地用于学习交流。如需用于其他用途请征得作者的同意。 原文链接:Using the Web Storage API - Mozilla Developer Network (英文) Web存储API提供了浏览器可以在本地安全存储键值对的一个机能,它比cookies更为直观。这篇文...

Proxy详解

一.Proxy基础 1. 什么是Proxy? Proxy是一个构造函数,可以通过它生成一个Proxy实例。 const proxy = new Proxy(target, handler); // target: 目标对象,待操作对象(可以是普通对象,数组,函数,proxy实例对象) // handler: 一个对象,最多可包含13个操作方法,也可以是空对...

pixijs shader 设置透明度的方法

pixijs shader 设置透明度的方法 precision mediump float; varying vec2 vTextureCoord; varying vec4 vColor; uniform sampler2D uSampler; uniform sampler2D noise; uniform float customUniform...

XML中的二进制文件的编码与解码[原创]

(一)把二进制文件放到XML中 using System;using System.Data;using System.IO;using System.Text;using System.Xml;using System.Configuration;using System.Collections;using System.Web;using Syste...