diff --git a/common/js/websocket.js b/common/js/websocket.js index 010d2c0..d9d8490 100644 --- a/common/js/websocket.js +++ b/common/js/websocket.js @@ -1,6 +1,6 @@ class webSocketUtils { constructor(url, time, params) { - this.socketTask = false; + this.socketTask = null; this.is_open_socket = false; //避免重复连接 this.url = url; this.params = params ? params : null; ////是否初始化请求 @@ -27,24 +27,31 @@ class webSocketUtils { connectSocketInit(data) { let _this = this; this.data = data; - - uni.connectSocket({ + console.log(this.url) + // #ifdef MP-WEIXIN + this.socketTask = uni.connectSocket({ + // #endif + // #ifdef MP-ALIPAY + my.connectSocket({ + // #endif url: this.url, - header: { - 'content-type': 'application/json' - }, success: (res) => { - console.log('建立websocketc成功...'); + console.log('创建websocketc成功...'); // uni.hideLoading(); // 返回实例 - this.socketTask = true; + return this.socketTask; }, fail: (res) => { console.log(res) } }); - uni.onSocketOpen((res) => { + // #ifdef MP-WEIXIN + this.socketTask.onOpen((res) => { + // #endif + // #ifdef MP-ALIPAY + my.onSocketOpen((res) => { + // #endif uni.hideLoading() this.connectNum = 1; console.log('WebSocket连接正常!==',res); @@ -57,21 +64,28 @@ class webSocketUtils { this.canReconnect = true; this.start(); // 注:只有连接正常打开中 ,才能正常收到消息 - - uni.onSocketMessage((e)=>{ + // #ifdef MP-WEIXIN + this.socketTask.onMessage((e) => { + // #endif + // #ifdef MP-ALIPAY + my.onSocketMessage((e)=>{ + // #endif // 字符串转json let res = JSON.parse(e.data); uni.$emit('message', res) // 普通socket信息处理 TODO }); - },(res)=>{ - - console.log(res) }); + // 监听连接失败,这里代码我注释掉的原因是因为如果服务器关闭后,和下面的onclose方法一起发起重连操作,这样会导致重复连接 + // #ifdef MP-WEIXIN uni.onSocketError((res) => { + // #endif + // #ifdef MP-ALIPAY + my.onSocketError((res) => { + // #endif console.log('网络断开,请检查!'); - this.socketTask = false; + this.socketTask = null; this.is_open_socket = false; // this.Close() this.canReconnect = true; @@ -111,17 +125,22 @@ class webSocketUtils { } }); // 这里仅是事件监听【如果socket关闭了会执行】 - uni.onSocketClose((res) => { + // #ifdef MP-WEIXIN + this.socketTask.onClose(() => { + // #endif + // #ifdef MP-ALIPAY + my.onSocketClose((res) => { + // #endif console.log("socket关闭了") - this.socketTask = false; + this.socketTask = null; clearInterval(this.heartbeatInterval); clearInterval(this.reconnectTimeOut); // #ifdef MP-ALIPAY // 支付宝小程序的ws连接问题,关闭连接时需关闭对于接受,防止关闭失败 - uni.offSocketMessage(); - uni.offSocketError(); - uni.offSocketOpen(); - uni.offSocketClose(); + my.offSocketMessage(); + my.offSocketError(); + my.offSocketOpen(); + my.offSocketClose(); // #endif this.is_open_socket = false; if (this.canReconnect) { @@ -134,41 +153,58 @@ class webSocketUtils { Close() { this.is_open_socket = true; this.canReconnect = false; - // if (this.socketTask) { - uni.closeSocket({ + + // #ifdef MP-WEIXIN + if (this.socketTask) { + this.socketTask.close({ success(res) { console.log('手动关闭成功'); - this.socketTask = false; - clearInterval(this.heartbeatInterval); - clearInterval(this.reconnectTimeOut); - // #ifdef MP-ALIPAY - // 支付宝小程序的ws连接问题,关闭连接时需关闭对于接受,防止关闭失败 - uni.offSocketMessage(); - uni.offSocketError(); - uni.offSocketOpen(); - uni.offSocketClose(); - // #endif - }, - fail: (res) => { - console.log('手动关闭失败==',res); - } }); + } + // #endif + // #ifdef MP-ALIPAY + my.closeSocket({ + success(res) { + console.log('手动关闭成功'); + // #ifdef MP-ALIPAY + // 支付宝小程序的ws连接问题,关闭连接时需关闭对于接受,防止关闭失败 + my.offSocketMessage(); + my.offSocketError(); + my.offSocketOpen(); + my.offSocketClose(); + // #endif - // } + }, + fail: (res) => { + console.log('手动关闭失败==',res); + } + }); + // #endif } //发送消息 send(data) { // console.log("发送消息---------->", data); // 注:只有连接正常打开中 ,才能正常成功发送消息 - // if (this.socketTask) { - uni.sendSocketMessage({ + + // #ifdef MP-WEIXIN + if (this.socketTask) { + this.socketTask.send({ data: JSON.stringify(data), - success(res) { + async success() { // console.log("消息发送成功"); }, }); - // } + } + // #endif + // #ifdef MP-ALIPAY + my.sendSocketMessage({ + data: JSON.stringify(data), + success(res) { + // console.log("消息发送成功"); + }, + }); + // #endif } //开启心跳检测 start(data) {