uni-app uni.onSocketOpen 会偶发性的不触发,经测试10次大概会有1次
uni-app uni.onSocketOpen 会偶发性的不触发,经测试10次大概会有1次
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Windows | win10 | HBuilderX |
示例代码:
import cache from ‘…/cache.js’ import config from ‘@/common/config.js’ var _this;
function socketInit(){ _this = this; _this.heartInfo = { heartTimer : null, //心跳计时器 heartNum : 8000, //心跳时间 socketFlag : false, //避免重复链接,是否链接握手成功 } _this.socketInfo = { isByMy : false, //是否手动关闭,false不是,true是 socketFlag : false, //开关,避免重复链接 ttTimer : null, ttNum : 4000, //延迟时间 } }
//第一步,初始化-建立链接 socketInit.prototype.init = function(type){ console.log("============== " + JSON.stringify(config.WEB_SOCKET_URL+"/im/"+cache.get_token())); uni.connectSocket({ url: config.WEB_SOCKET_URL+"/im/"+cache.get_token(), success(res) { _this.socketInfo.isByMy = false; console.log(“初始化websoket==========” + JSON.stringify(res)); _this.socketOpen(); }, fail(error){ console.log("初始化error======= " + JSON.stringify(error)); _this.socketReconnect() //进行重连 } }); }
uni.onSocketError(function (res) {
console.log(‘WebSocket连接打开失败,请检查!’);
console.log("_this.socketInfo.isByMy: " + JSON.stringify(_this.socketInfo.isByMy));
if(!_this.socketInfo.isByMy){
_this.socketInfo.socketFlag = false;
_this.socketReconnect()
}
});
uni.onSocketClose(function(res){
console.log("关闭: “);
console.log(”_this.socketInfo.isByMy: " + JSON.stringify(_this.socketInfo.isByMy));
if(!_this.socketInfo.isByMy){
_this.socketInfo.socketFlag = false;
_this.socketReconnect()
}
})
//第二部,打开socket socketInit.prototype.socketOpen = function(){ uni.onSocketOpen(function (res) { console.log(‘WebSocket聊天连接已打开!’); uni.$emit(‘homeSocketInit’) uni.$emit(‘chatSocketInit’) _this.socketInfo.isByMy = false; _this.start() }); }
//断开重连 socketInit.prototype.socketReconnect = function(){ clearInterval(_this.heartInfo.heartTimer); //清除心跳计时; if (_this.socketInfo.socketFlag){ return } _this.socketInfo.socketFlag = true; //延迟4秒重连 clearTimeout(_this.socketInfo.ttTimer); _this.socketInfo.ttTimer = setTimeout(function () { _this.init() _this.socketInfo.socketFlag = false; }, 4000); }
//心跳===============每个5秒,像服务器请求一次,是否链接正常 socketInit.prototype.start = function(){ _this.heartInfo.heartTimer = setInterval(()=>{ const obj = { msg_cnt : “ping”, msg_type : “heartbeat” } uni.sendSocketMessage({ data : JSON.stringify(obj), success:function(data){ console.log("心跳正常: " ); } }); },_this.heartInfo.heartNum) }
//手动关闭 socketInit.prototype.close = function(type){ _this.socketInfo.isByMy = true; _this.socketInfo.socketFlag = true; clearInterval(_this.heartInfo.heartTimer); clearTimeout(_this.socketInfo.ttTimer); uni.closeSocket(); }
export default new socketInit()
### 操作步骤:
这是首页home.vue 的代码
onLoad() {
//每次初始化先关闭两个socket链接,
_this.$socket.close('isByOur');
_this.SocketTaskSys.close();
//然后在链接
setTimeout(()=>{_this.$socket.init();},600)
}
预期结果:
每次关闭socket链接后,重新打开uni.onSocketOpen 能正常执行
### 实际结果:
会偶发性的不触发,经测试10次 大概会有1次;
有时候会突然来一次就是偶发性的;
bug描述:
vue 页面我创建了一个全局的uni.connectSocket 链接, 首页进来又创建了另外一个_this.SocketTaskSys = uni.connectSocket({})这种写法的链接。
意思就是 首页初始化的时候,我创建了两个socket链接,但是uni.onSocketOpen 每次进到这个界面会偶发性的不会触发,经测试10次会有 1次 不会走uni.onSocketOpen 里面的方法;
更多关于uni-app uni.onSocketOpen 会偶发性的不触发,经测试10次大概会有1次的实战教程也可以访问 https://www.itying.com/category-93-b0.html

