LinPro
LinPro
  • 发布:2021-09-16 16:52
  • 更新:2021-09-16 17:15
  • 阅读:1695

uni webSocket onMessage onSocketMessage 无效

分类:uni-app

环境:Android 默认基座 真机调试环境

uni.connectSocket() 成功

uni 其余所有websocket on的监听方法都可触发

仅onMessage onSocketMessage 无反应,接收不到消息

   // 启动webSocket  
    startSocket(url) {  
        this.setConsole("WebSocket链接:", this.socketIP + this.socketPort + url);  
        this.socketUrl = url;  
        let _this = this;  
        this.SocketTask = uni.connectSocket({  
            url: this.socketIP + this.socketPort + url,  
            success: (res) => {  
                _this.setConsole("socketTask 对象:", _this.SocketTask);  

            },  
            fail: (e) => {  
                _this.setConsole("WebSocket连接失败:", e);  
            },  
            complete: () => {}  
        })  

        // 监听服务器推送消息   
        _this.SocketTask.onMessage((msg) =>{  
            _this.setConsole("----------websocket onMessage success-----:", msg);  
        });  

        // 连接成功触发  
        _this.SocketTask.onOpen((res) => {  
            _this.setConsole('WebSocket已打开', res);  

            // 清理重连轮询  
            clearInterval(_this.defibrillationTypeList.connectSocket);  
            _this.defibrillationTypeList.connectSocket = null;  

            //断线后重连几次  
            _this.reconnectCount = 3;  
            // 30s发送一次伪心跳  
            _this.heartbeatInterval = setInterval(() => {  
                // send锁控制ping只能在,send闲置或者send周期 > ping周期的情况下,ping才能获得send消息的能力  
                if (!_this.sendLock) {  
                    _this.sendSocketMsg("ping")  
                }  
            }, 30000)  

            // #ifdef APP-PLUS  
            // 启动网络状态监听器  
            _this.startNetWatch();  

            // 启动定位监听器  
            _this.startLocation();  
            // #endif  

        });  

        // 监听连接关闭  
        _this.SocketTask.onClose((e) => {  
            _this.setConsole('WebSocket监听关闭事件', e);  
            // 清理定时任务  
            clearInterval(_this.heartbeatInterval)  
            clearInterval(_this.trackUploadInterval)  
            // #ifdef APP-PLUS  
            // 清理定位监听器  
            _this.closeLocation();  
            // #endif  
            _this.customDebounce(() => { // 断线重连  
                if (!_this.isSocketClose) {  
                    if (_this.reconnectCount <= 0) {  
                        //如果重连次数为0,则清理定时器  
                        clearInterval(_this.defibrillationTypeList.connectSocket);  
                        _this.defibrillationTypeList.connectSocket = null;  
                    } else {  
                        _this.setConsole("", `第${3 - _this.reconnectCount + 1}次断线重连中...`)  
                        _this.reconnectCount = _this.reconnectCount - 1;  
                        _this.startSocket(_this.socketUrl);  
                    }  
                }  
            }, "connectSocket", 5000)  
        });  

        // 监听连接失败  
        _this.SocketTask.onError((res) => {  
            _this.setConsole('WebSocket连接失败,请检查!', '');  
            // 清理定时任务  
            clearInterval(_this.heartbeatInterval)  
            clearInterval(_this.trackUploadInterval)  
            // #ifdef APP-PLUS  
            // 清理定位监听器  
            _this.closeLocation();  
            // #endif  
            //如果不是人为关闭的话,进行重连  
            _this.customDebounce(() => { // 断线重连  
                if (!_this.isSocketClose) {  
                    if (_this.reconnectCount <= 0) {  
                        //如果重连次数为0,则清理定时器  
                        clearInterval(_this.defibrillationTypeList.connectSocket);  
                        _this.defibrillationTypeList.connectSocket = null;  
                    } else {  
                        _this.setConsole("", `第${3 - _this.reconnectCount + 1}次断线重连中...`)  
                        _this.reconnectCount = _this.reconnectCount - 1;  
                        _this.startSocket(_this.socketUrl);  

                    }  
                }  
            }, "connectSocket", 5000)  
        });  

    }  

    // 利用已连接成功的SocketTask对象进行socket消息提交  
    sendSocketMsg(msgClass) {  
        let _this = this;  
        let msg = JSON.stringify(msgClass)  
        if (typeof msgClass === 'string') {  
            msg = msgClass  
        }  
        // send消息前锁定sendLock 变量,其余send根据需要,是否争用send 来用sendLock进行判断  
        _this.sendLock = true;  
        _this.setConsole('WebSocket sendMsg:', msg);  
        //通过 WebSocket 连接发送数据  
        _this.SocketTask.send({  
            data: msg,  
            success: (res) => {  
                _this.setConsole("WebSocket send响应成功:", res);  
                _this.sendLock = false;  
            },  
            fail: (err) => {  
                _this.setConsole("WebSocket send响应异常:", err);  
                _this.sendLock = false;  
                _this.SocketTask.close();  
            }  
        });  

    }  

    // 函数防抖处理方法  
    customDebounce(rf, defibrillationType, timer) {  
        let _this = this;  
        this.setConsole('防抖间隔:', timer);  
        this.setConsole('防抖触发:', defibrillationType);  
        this.setConsole('防抖缓存:', _this.defibrillationTypeList[defibrillationType]);  
        if (_this.defibrillationTypeList[defibrillationType] != null) {  
            clearInterval(_this.defibrillationTypeList[defibrillationType]);  
        }  
        _this.defibrillationTypeList[defibrillationType] = setInterval(rf, timer);  
    }
2021-09-16 16:52 负责人:无 分享
已邀请:
s***@yeah.net

s***@yeah.net

同问

  • LinPro (作者)

    已解决,url 参数末尾拼接的路径有误,导致无法连接到正确的消息通道

    2021-09-23 11:33

该问题目前已经被锁定, 无法添加新回复