为什么emqx订阅消息时通过websocket连接时不停地断开重连?

环境信息

  • EMQ X 版本:4.3
  • 操作系统及版本:centos 7
  • 其他

问题描述

订阅消息,用websocket方式连接时,会不停地断开重连,很不稳定。

配置文件及日志

bash-5.1$ cat emqx.conf 
node.name = emqx@127.0.0.1
node.cookie = emqxsecretcookie
node.data_dir = data
node.global_gc_interval = 15m
node.crash_dump = log/crash.dump
node.dist_listen_min = 6369
node.dist_listen_max = 6369
node.backtrace_depth = 16
allow_anonymous = true
acl_nomatch = allow
acl_file = etc/acl.conf
enable_acl_cache = on
acl_cache_max_size = 32
acl_cache_ttl = 1m
acl_deny_action = ignore
flapping_detect_policy = 30, 1m, 5m
mqtt.max_packet_size = 1MB
mqtt.max_clientid_len = 65535
mqtt.max_topic_levels = 128
mqtt.max_qos_allowed = 2
mqtt.max_topic_alias = 65535
mqtt.retain_available = true
mqtt.wildcard_subscription = true
mqtt.shared_subscription = true
mqtt.ignore_loop_deliver = false
mqtt.strict_mode = false
plugins.etc_dir = etc/plugins/
plugins.loaded_file = data/loaded_plugins
plugins.expand_plugins_dir = etc/plugins/
broker.sys_interval = 1m
broker.sys_heartbeat = 30s
broker.session_locking_strategy = quorum
broker.shared_subscription_strategy = random
broker.shared_dispatch_ack_enabled = false

broker.route_batch_clean = off
broker.perf.trie_compaction = false
license.file = etc/emqx.lic

include etc/rpc.conf
include etc/logger.conf
include etc/listeners.conf
include etc/zones.conf
include etc/cluster.conf
include etc/sys_mon.conf

image

你检查下你的客户端代码,有没有设置keepalive,且定时发送心跳

我也遇到了,keepalive是有配置的,订阅的topic每个100ms就有消息发送过来,当时wss会话每个几秒就会断开然后重连。

最后是怎么解决的啊?

没有找到原因,解决不了,我也在寻求答案,等待官方给答复吧

如何定时发送心跳?有code demo吗?

客户端有个配置keepalive,这个就是配置发送心跳信号的时间。

请教一下,keepalive的时间单位是什么?如果发送方每100ms推送一次数据,那么订阅方的keepalive应该设置为多少合适?

我记得客户端有个参数配置,多久没有消息接收或发送处于空闲状态,会话就会断开,设置的keealive小于这个时间就可以了,具体看看客户端的配置说明。keepalive的单位是ms。

但是我的频繁断开重连的问题,并不是keepalive的问题。你看看能不能解决你的问题吧