MQTT超时断连问题处理方式无效,删除相关代码
调整重连次数默认值
This commit is contained in:
parent
4187eb2c4f
commit
6ec177342e
@ -46,7 +46,7 @@ export interface MessageCliOption {
|
||||
*/
|
||||
retryPeriod?: number;
|
||||
/**
|
||||
* 重试次数,默认30次
|
||||
* 重试次数,默认100次
|
||||
*/
|
||||
retryTimes?: number;
|
||||
}
|
||||
@ -67,7 +67,7 @@ const DefaultStompOption: CompleteMessageCliOption = {
|
||||
connectTimeout: 30 * 1000,
|
||||
heartbeat: 60,
|
||||
retryPeriod: 2 * 1000,
|
||||
retryTimes: 30,
|
||||
retryTimes: 100,
|
||||
};
|
||||
|
||||
export interface IMessageClient extends EventEmitter<MessageClientEvents> {
|
||||
|
@ -5,7 +5,6 @@ import { CompleteMessageCliOption } from './MessageBroker';
|
||||
export class MqttMsgClient extends MessageClient {
|
||||
cli: mqtt.MqttClient;
|
||||
retryTimes = 0;
|
||||
timer: NodeJS.Timeout | undefined;
|
||||
subMsgHandler: Map<string, HandleMessage> = new Map();
|
||||
|
||||
constructor(options: CompleteMessageCliOption) {
|
||||
@ -26,12 +25,6 @@ export class MqttMsgClient extends MessageClient {
|
||||
console.log('MQTT 连接成功!');
|
||||
this.retryTimes = 0; // 连接成功,重置
|
||||
this.emit('connected', packet);
|
||||
// mqtt本身的定时发送心跳在浏览器页面后台运行时似乎会发生计时变慢?最终导致心跳超时断连:
|
||||
// WebSocket connection to 'ws://ip:port/mqtt' failed: Close received after close
|
||||
// 在此使用自定义定时发送心跳暂时看可以处理超时断连问题
|
||||
this.timer = setInterval(() => {
|
||||
(this.cli as any)._sendPacket({ cmd: 'pingreq' });
|
||||
}, (options.heartbeat / 2) * 1000);
|
||||
});
|
||||
this.cli.on('disconnect', (packet) => {
|
||||
console.log('MQTT 连接断开!');
|
||||
@ -43,6 +36,7 @@ export class MqttMsgClient extends MessageClient {
|
||||
});
|
||||
this.cli.on('reconnect', () => {
|
||||
this.retryTimes += 1;
|
||||
console.log(`MQTT第 ${this.retryTimes} 次尝试重连`);
|
||||
if (this.retryTimes > options.retryTimes) {
|
||||
try {
|
||||
this.cli.end();
|
||||
@ -106,7 +100,6 @@ export class MqttMsgClient extends MessageClient {
|
||||
close(): void {
|
||||
try {
|
||||
console.debug('MQTT关闭执行');
|
||||
clearInterval(this.timer);
|
||||
this.cli.end(true, () => {
|
||||
console.debug('MQTT 消息客户端关闭成功');
|
||||
this.subMsgHandler.clear();
|
||||
|
Loading…
Reference in New Issue
Block a user