• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

前端js react 全局websocket 连接类 带订阅发布以和使用方法

武飞扬头像
宋德武
帮助1

连接websocket js类,整合聚合发布,其它任意文件进行订阅


//@ts-ignore
import {
    toLogin } from '@/utils/login';
import {
    message } from 'antd';
//@ts-ignore
import SockJS from 'sockjs-client';
import Stomp from 'stompjs';
let userId = localStorage.getItem('userId')

interface subscribes {
   
  name: string,
  subscribe: any
}

export class WS {
   

  //链接地址
  socketAddress: string = APP_URL.socketAddress;
  stompClient: any = null;
  socket: any = null;
  messageData: any[] = [];
  conCount: number = 0;
  timerCon: any = null;
  timerStomp:any = null;
  stompCount:number = 0;
  connected: boolean = false;
  subscribeFuns: Map<Function, Function> = new Map();
  subscribes: subscribes[] = []; //{name:'message',subscribe:subscribeFun}
  //链接地址
  urls: {
    name: string, url: string }[] = [
    {
    name: 'message', url: `/user/${
     userId}/topic/message` }, //基地变更消息
    {
    name: 'overTimeMessage', url: `/user/${
     userId}/topic/token/expiration` }, // 离线消息
    {
    name: 'dropLineMessage', url: `/user/${
     userId}/topic/login/repeat` }, // 下线
    {
    name: 'storageMessage', url: `/topic/Storage/message` },  //入库申请
    {
    name: 'chatMessage', url: `/user/${
     userId}/topic/chat` },//聊天消息
  ]

  constructor() {
   
    this.connectWs();

  }

  /**
   * 订阅消息
   * @param fn
   */
  subscribeMessage(fn: Function) {
   
    this.subscribeFuns.set(fn, fn)
  }

  //订阅所有
  subscribeAll(
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgekhge
系列文章
更多 icon
同类精品
更多 icon
继续加载