英文:
How to change MQTT keepAlive(handshake) Interval between the client and the broker in Golang?
问题
我是你的中文翻译助手,以下是你要翻译的内容:
我对GO和MQTT都不太熟悉。在我启动客户端c := MQTT.NewClient(opts) c.Start()之后,每30秒客户端和代理之间都会出现握手流量。我只需要调整这个间隔或者取消握手。
英文:
I'm new in both GO and MQTT. After I started the client c := MQTT.NewClient(opts) c.Start() and until it disconnected each 30 sec. handshake traffic between client and broker showed up. I just need to adjust this interval or cancel handshake at all.
答案1
得分: 4
keepAlive '握手' 是必需的,不能禁用,这是代理知道客户端仍然连接的方式。
您可以通过在将其传递给 NewClient 方法之前,在 opts 对象上调用 SetKeepAlive 来更改 keepalive 超时时间。
该方法接受一个以秒为单位的值,表示每个 keepAlive 数据包之间的时间间隔。
使用这里的示例代码,您可以添加一行代码将 KeepAlive 超时时间设置为 30 秒。
...
opts := MQTT.NewClientOptions().SetBroker("tcp://iot.eclipse.org:1883")
opts.SetClientId("go-simple")
opts.SetTraceLevel(MQTT.Off)
opts.SetDefaultPublishHandler(f)
opts.SetKeepAlive(30)
// 使用上述 ClientOptions 创建并启动客户端
c := MQTT.NewClient(opts)
_, err := c.Start()
if err != nil {
panic(err)
}
...
英文:
The keepAlive 'handshake' is required it can not be disabled, it is how the broker knows the client is still connected.
You can change the keepalive timeout by calling SetKeepAlive on the opts object before passing it to the NewClient method.
This method takes a value in seconds for the time between each keepAlive packet.
Using the sample code here, you would add a line like this to set the KeepAlive timeout to 30 seconds.
...
opts := MQTT.NewClientOptions().SetBroker("tcp://iot.eclipse.org:1883")
opts.SetClientId("go-simple")
opts.SetTraceLevel(MQTT.Off)
opts.SetDefaultPublishHandler(f)
opts.SetKeepAlive(30)
//create and start a client using the above ClientOptions
c := MQTT.NewClient(opts)
_, err := c.Start()
if err != nil {
panic(err)
}
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论