未完待续_
未完待续_
  • 发布:2019-09-15 16:00
  • 更新:2019-09-16 12:15
  • 阅读:2496

uni-app uni.connectSocket第一次链接不上

分类:uni-app

用的uni-app框架 每次打包完第一次进入应用 uni.connectSocket链接socket 都会链接超时而失败,进入fail里里面 然后第二次进入就好了 代码如下 用的wss链接
getSocket() {
let _self = this;
let message_source = 'shopapp';
uni.connectSocket({
url: _self.$_common.socketUrl,
success(res) {
// console.log('连接中。。。'+JSON.stringify(res))
},
fail(err) {
// console.log('连接失败'+JSON.stringify(err))
}
});
uni.onSocketOpen(function() {
let mem_id = uni.getStorageSync('mem_id');
let json_1 = { mem_id: mem_id };

            let data = { message_head: 'login', message_source: message_source, data: json_1 };  
            //登录  
            // console.log(JSON.stringify(data));  
            uni.sendSocketMessage({  
                data: JSON.stringify(data),  
                success: function(res) {},  
                fail: function(err) {  
                    // console.log(JSON.stringify(err));  
                }  
            });  
        });  
        uni.onSocketMessage(function(res) {  
            let data = JSON.parse(res.data);  

            switch (data.message_head) {  
                case 'loginResponse':  
                    _self.chat_id = data.chat_id;  
                    let run = {  
                        message_head: 'run',  
                        message_source: message_source,  
                        data: {  
                            message: 'I_am_alive',  
                            chat_id: _self.chat_id  
                        }  
                    };  
                    // 心跳  
                    setInterval(function() {  
                        uni.sendSocketMessage({  
                            data: JSON.stringify(run)  
                        });  
                    }, 45000);  
                    common.chat_id = data.chat_id;  

                    break;  
                default:  
                    break;  
            }  
            if (data.numAll > 0) {  
                uni.setTabBarBadge({  
                    index: 1,  
                    text: data.numAll.toString()  
                });  
            }  

            common.msgSocket = {  
                OrderNum: data.OrderNum,  
                SystemNum: data.SystemNum,  
                PlatformNum: data.PlatformNum,  
                ChatNoReadNum: data.ChatNoReadNum  
            }  
            // console.log(res.data);  
        });  
        uni.onSocketClose(function(res) {  
            setTimeout(function(){  
                _self.getSocket();  
            },1000)  
            // console.log('WebSocket 已关闭!');  
        });  
        uni.onSocketError(function (res) {  
            setTimeout(function(){  
                _self.getSocket();  
            },1000)  
          // console.log('WebSocket连接打开失败,请检查!');  
        });  
    }
2019-09-15 16:00 负责人:无 分享
已邀请:
DCloud_UNI_GSQ

DCloud_UNI_GSQ

真机预览是否有此问题?iOS端还是安卓?

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