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