代码如下,H5是正常的,安卓不正常,看了社区的相关问题,也没有解决,请问有解决的办法吗?或者有大佬可以帮忙分析一下原因吗?跪求帮助,谢谢
补充一下:我在socket的地址后面拼接了token,我看到token,存在一些特殊字符,例如:- 和 . ,没有遇到另外一个话题里提到的 I
import { SocketTypes } from "../constant";
import { beforeRequest, beforeResponse } from "./interceptor";
export default class SocketInstance {
//socket地址
private url: string;
//socket连接请求头
private header?: RequestOptions.Header;
//socketOpen 连接状态
private socketOpenStatus: boolean = false;
//连接尝试次数
private connectNum: number = 0;
//最大尝试次数
private connectMaxNum: number = 50;
//请求数据栈,存放未发送数据
private socketMsgQueue: Array<SocketOptions.Data> = [];
//心跳检测Timer
private heartTimer: NodeJS.Timer | undefined = undefined;
//消息接收的回调
callBackMessage?: (result: UniApp.OnSocketMessageCallbackResult) => void;
/**
* socket连接类
* @param url 地址
* @param header
* @param callBackMessage 可选回调,在socket类接收到消息后,向外部推出消息
*/
constructor(
url: string,
header?: RequestOptions.Header,
callBackMessage?: (result: UniApp.OnSocketMessageCallbackResult) => void,
) {
this.url = url;
this.header = header;
this.callBackMessage = callBackMessage;
//初始化socket链接
this.initSocket();
}
/**
* 创建socket连接
*/
private async connectSocket() {
try {
await uni.connectSocket({
url: this.url,
header: this.header,
});
} catch (e) {
console.warn("链接失败");
//连接失败,再次尝试连接,超过规定次数,不再连接
if (this.connectNum < this.connectMaxNum) {
this.connectNum += 1;
await this.connectSocket();
} else {
this.connectNum = 0;
}
}
}
private initSocket() {
//开启链接
this.connectSocket();
//socket连接成功
uni.onSocketOpen(this.socketOpen.bind(this));
//链接出错
uni.onSocketError(this.socketError.bind(this));
//链接断开
uni.onSocketClose(this.socketClose.bind(this));
//接收消息
uni.onSocketMessage(this.socketMessage.bind(this));
}
/**
* 发送websocket数据
* @param data
*/
send(data: SocketOptions.Data) {
//链接已经打开,直接发送数据
if (this.socketOpenStatus) {
//响应数据拦截器,可以做一些统一额外处理
const interceptorResult = beforeRequest(data);
uni.sendSocketMessage({
data: JSON.stringify(interceptorResult),
});
} else {
//链接未打开,将数据添加到数据队列
this.socketMsgQueue.push(data);
}
}
/**
* 链接心跳检测
*/
private sendHeartPack() {
this.heartTimer = setInterval(() => {
this.send({
type: SocketTypes.HEARTBEAT_DETECTION,
});
}, 10000);
}
//接收socket消息
private socketMessage(result: UniApp.OnSocketMessageCallbackResult) {
//执行向外暴露数据的回调
if (this.callBackMessage) {
//响应数据拦截器,可以做一些额外处理
const interceptorResult = beforeResponse(result);
this.callBackMessage(interceptorResult);
}
}
/**
* socket连接成功
*/
private socketOpen(result: UniApp.OnSocketOpenCallbackResult) {
this.socketOpenStatus = true;
//连接成功,开启心跳检测
this.sendHeartPack();
// console.log("socket连接成功", result);
console.warn("socket连接成功", result);
//连接成功,发送所有暂存数据,清空数据栈
for (let i = 0; i < this.socketMsgQueue.length; i += 1) {
this.send(this.socketMsgQueue[i]);
}
this.socketMsgQueue = [];
}
/**
* socket连接失败
*/
private socketError(result: UniApp.GeneralCallbackResult) {
this.socketOpenStatus = false;
//连接失败,关闭心跳检测
clearInterval(this.heartTimer);
// console.log("socket连接失败", result);
console.warn("socket连接失败", result);
//去重试连接
this.connectSocket();
}
/**
* socket连接关闭
* @param result
*/
private socketClose(result: UniApp.GeneralCallbackResult) {
this.socketOpenStatus = false;
//连接关闭,关闭心跳检测
clearInterval(this.heartTimer);
// console.log("socket连接关闭", result);
console.warn("socket连接关闭", result);
//去重试连接
this.connectSocket();
}
}
export * from "./types";
export * from "./socketCallBack";
MT123
解决了吗我也遇到这个问题uni.connectSocket 显示连接成功 但是open方法就是不触发
2024-05-29 22:42