F_hawk189
F_hawk189
  • 发布:2019-04-12 09:51
  • 更新:2020-04-07 17:47
  • 阅读:4882

uni-app的websocket通信如何重复执行以及如何超时

分类:uni-app

使用websocket进行通信,但是connect函数需要在代码段A执行后再执行,代码段A是异步的,所以可能出现代码段A未执行完毕导致connect之后代码失败。并且在sendSocketMessage后,服务端会过一小会返回数据,这时,onSocketMessage可能就会出现失败,如何对这块执行顺序还有管控,就是失败后进行重试,然后设置超时时间。

                                        代码段A  

                                         this.sleep(3000);  
                     console.log("开始connect.........")  
                    uni.connectSocket({  
                        url: 'ws://192.168.4.1:8888',  
                        success: function() {  
                            console.log("WebSocket连接成功");  

                            uni.onSocketOpen(function(res) {  
                                console.log('WebSocket连接已打开!');  
                                uni.sendSocketMessage({  
                                    data: that.data,  
                                    success: function() {  
                                        console.log("数据发送成功,数据为:" + that.data);  

                                        //-----------------------------------------  
                                        console.log("数据发送结束,开始接收");  
                                        that.sleep(2000);  
                                        uni.onSocketMessage(function(res) {  
                                            isReceive = true;  
                                            console.log('收到服务器内容:' + res.data);  
                                            that.closeWifi();  
                                            uni.closeSocket({  
                                                success: function() {  
                                                    console.log("websocket关闭成功");  
                                                },  
                                                fail: function() {  
                                                    console.log("websocket关闭失败");  
                                                }  
                                            })  
                                            if (res.data == '1') {    
                                                //that.goOn();  
                                                that.sleep(2000);  
                                                uni.hideLoading();  
                                                that.sendInfo();  
                                            }  
                                            else{  
                                                uni.showToast({  
                                                    title: "设备故障",  
                                                    icon:'none'  
                                                })  
                                                uni.hideLoading();  
                                            }  
                                        })  
                                        //--------------------------------------  

                                    },  
                                    fail: function() {  
                                        console.log("数据发送失败");  
                                    }  
                                });  

                                uni.onSocketClose(function(res){  
                                    console.log("websocket已关闭");  
                                    if (isReceive == false) {  
                                        uni.hideLoading();  
                                        uni.showToast({  
                                            title: "通信失败",  
                                            icon:'none'  
                                        })  
                                    }  
                                    else {  
                                        // uni.hideLoading();  
                                    }  
                                })  

                            })  
                        },  
                        fail: function(){  
                            uni.hideLoading();  
                            uni.showToast({  
                                title: "设备初始化失败",  
                                icon:'none'  
                            })  
                        },  
                    // )};   

                })  
                    uni.onSocketError(function(res) {  
                        uni.hideLoading();  
                        console.log('WebSocket连接打开失败,请检查!');  
                        uni.showToast({  
                            title: "设备初始化失败",  
                            icon: "none"  
                        })  
                        return false;  
                    });
2019-04-12 09:51 负责人:无 分享
已邀请:
jonychen1

jonychen1

做websocket开发,可以自己搭建,也可以试试第三方的websocket框架。我近期在使用的GoEasy这个框架,在应对我们项目这种突发性的高并发需求上,表现不错。有websocket使用需求的也可以去看看,地址:https://www.goeasy.io/

  • 1***@qq.com

    在公众平台使用 会连接超时

    2020-05-20 10:24

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