吉安感知网项目-前端
罗广辉
2026-02-27 e00da17ae2adaecf1cdf2d5128e357a50607bc90
uniapps/work-app/src/utils/websocket.js
@@ -5,6 +5,7 @@
    this.connected = false
    this.url = ''
    this.userId = ''
    this.token = ''
    this.onMessageCallback = null
    this.onOpenCallback = null
    this.onCloseCallback = null
@@ -13,14 +14,15 @@
  }
  // 初始化WebSocket连接
  init(userId, url) {
  init(userId, url, token) {
    if (!url) {
      console.error('WebSocket初始化失败:缺少url')
      return
    }
    this.userId = userId || ''
    this.url = url + encodeURIComponent(this.userId)
    this.token = token || ''
    this.url = url
    this.connect()
  }
@@ -29,10 +31,10 @@
    try {
      // 关闭现有连接
      this.close()
      // 使用uni-app的WebSocket API
      this.socketTask = uni.connectSocket({
        url: this.url,
            protocols: [this.token],
        success: () => {
          // console.log('WebSocket连接已请求')
        },
@@ -129,7 +131,7 @@
      if (this.connected) {
        this.send('ping', 'system')
      }
    }, 2000)
    }, 25000)
  }
  // 停止心跳检测