【问题标题】:How to change MQTT keepAlive(handshake) Interval between the client and the broker in Golang?如何更改 Golang 中客户端和代理之间的 MQTT keepAlive(handshake) 间隔?
【发布时间】:2014-09-19 20:42:08
【问题描述】:

我是 GO 和 MQTT 的新手。在我启动客户端 c := MQTT.NewClient(opts) c.Start() 之后,直到它每 30 秒断开一次。客户端和代理之间的握手流量出现了。我只需要调整这个间隔或者完全取消握手。

【问题讨论】:

    标签: go keep-alive mqtt handshake


    【解决方案1】:

    keepAlive“握手”是必需的,它不能被禁用,这是代理知道客户端仍然连接的方式。

    在将 opts 对象传递给 NewClient 方法之前,您可以通过调用 SetKeepAlive 来更改 keepalive 超时。

    此方法以秒为单位获取每个 keepAlive 数据包之间的时间值。

    使用示例代码here,您可以添加这样一行来将 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)
    
      //create and start a client using the above ClientOptions
      c := MQTT.NewClient(opts)
      _, err := c.Start()
      if err != nil {
        panic(err)
      }
      ...
    

    【讨论】:

    • 我想你会想要opts.SetKeepAlive(30 * time.Second),因为它需要time.Duration,(裸 30 可能会被强制转换为 30 纳秒,它本身可能被客户端或服务器端解释为“从不”或“默认”或“最小值”,如 5 秒或其他东西)。这是一个过时的答案,所以也许界面可能已经从秒数左右变成了time.Duration
    • @hardillb 您可以通过将 keepAlive 握手的值设置为零来禁用它,请参见此处:hivemq.com/blog/mqtt-essentials-part-10-alive-client-take-over
    猜你喜欢
    • 2019-09-26
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    相关资源
    最近更新 更多