|
|
@@ -58,6 +58,8 @@ class AsyncTask {
|
|
|
|
|
|
/** MTP2.0 长链通信类 */
|
|
|
export class MTP2WebSocket<T extends Package40 | Package50> {
|
|
|
+ /** 初始默认重连间隔时长 */
|
|
|
+ private defaultReconnectInterval = 5 * 1000;
|
|
|
/** 服务端地址 */
|
|
|
host;
|
|
|
/** 当前连接状态 */
|
|
|
@@ -65,7 +67,7 @@ export class MTP2WebSocket<T extends Package40 | Package50> {
|
|
|
/** 重连次数 */
|
|
|
reconnectCount = 0;
|
|
|
/** 重连间隔时长,默认为5秒 */
|
|
|
- reconnectInterval = 5 * 1000;
|
|
|
+ reconnectInterval = this.defaultReconnectInterval;
|
|
|
|
|
|
/** 连接成功的回调 */
|
|
|
onConnected?: (obj: MTP2WebSocket<T>) => void;
|
|
|
@@ -84,6 +86,8 @@ export class MTP2WebSocket<T extends Package40 | Package50> {
|
|
|
private packageType;
|
|
|
/** WebSocket 对象 */
|
|
|
private socket?: WebSocket;
|
|
|
+ /** 连接准备完成 */
|
|
|
+ private readyState?: Promise<MTP2WebSocket<T>>;
|
|
|
/** 当前流水号 */
|
|
|
private currentSerial = 1;
|
|
|
/** 默认请求超时时长(秒) */
|
|
|
@@ -105,11 +109,9 @@ export class MTP2WebSocket<T extends Package40 | Package50> {
|
|
|
private timeoutTimer = 0;
|
|
|
/** 心跳间隔时间,默认为10秒 */
|
|
|
private beatInterval = 10 * 1000;
|
|
|
- /** 心跳回复超时时间,默认为15秒 */
|
|
|
+ /** 心跳回复超时时间,默认为15秒 */
|
|
|
private beatTimeoutInterval = 15 * 1000;
|
|
|
|
|
|
- private readyState?: Promise<MTP2WebSocket<T>>;
|
|
|
-
|
|
|
/**
|
|
|
* 构造函数
|
|
|
* @param packageType 报文类型,0 - 4.0行情报文,1 - 5.0交易报文
|
|
|
@@ -182,6 +184,7 @@ export class MTP2WebSocket<T extends Package40 | Package50> {
|
|
|
this.stopBeatTimer();
|
|
|
this.uuid = v4();
|
|
|
this.reconnectCount = 0;
|
|
|
+ this.reconnectInterval = this.defaultReconnectInterval;
|
|
|
this.socket?.close();
|
|
|
this.reset(); // 不等待 ws.onclose 响应强制重置实例
|
|
|
}
|
|
|
@@ -464,7 +467,7 @@ export class MTP2WebSocket<T extends Package40 | Package50> {
|
|
|
this.stopBeatTimer();
|
|
|
if (this.connState !== 'Connecting') {
|
|
|
this.reconnectCount++;
|
|
|
- console.log(this.packageType, this.host, `5秒后将进行第${this.reconnectCount}次重连`);
|
|
|
+ console.log(this.packageType, this.host, `${this.reconnectInterval / 1000}秒后将进行第${this.reconnectCount}次重连`);
|
|
|
|
|
|
this.reconnectTimer = window.setTimeout(() => {
|
|
|
this.readyState = undefined;
|
|
|
@@ -473,10 +476,12 @@ export class MTP2WebSocket<T extends Package40 | Package50> {
|
|
|
this.conn().then(() => {
|
|
|
// 重连成功
|
|
|
this.reconnectCount = 0;
|
|
|
+ this.reconnectInterval = this.defaultReconnectInterval;
|
|
|
this.onReconnectChangeState && this.onReconnectChangeState(this, ReconnectChangeState.ReconnectSuccessed);
|
|
|
}).catch(() => {
|
|
|
if (this.reconnectCount) {
|
|
|
console.warn(this.packageType, this.host, `第${this.reconnectCount}次重连失败`);
|
|
|
+ this.reconnectInterval += this.defaultReconnectInterval; // 重连间隔时间每次递增5秒
|
|
|
this.onReconnectChangeState && this.onReconnectChangeState(this, ReconnectChangeState.FailAndWaitPeriod);
|
|
|
}
|
|
|
})
|