| | |
| | | this.connected = false |
| | | this.url = '' |
| | | this.userId = '' |
| | | this.token = '' |
| | | this.onMessageCallback = null |
| | | this.onOpenCallback = null |
| | | this.onCloseCallback = null |
| | |
| | | } |
| | | |
| | | // 初始化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() |
| | | } |
| | | |
| | |
| | | try { |
| | | // 关闭现有连接 |
| | | this.close() |
| | | |
| | | // 使用uni-app的WebSocket API |
| | | this.socketTask = uni.connectSocket({ |
| | | url: this.url, |
| | | protocols: [this.token], |
| | | success: () => { |
| | | // console.log('WebSocket连接已请求') |
| | | }, |