如何在Golang中更改客户端和代理之间的MQTT keepAlive(握手)间隔?

huangapple go评论70阅读模式
英文:

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)
  }
  ...

huangapple
  • 本文由 发表于 2014年9月20日 04:42:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/25942101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定