dounaidu0204
dounaidu0204
2014-09-19 20:42

如何在Golang中更改客户端与代理之间的MQTT keepAlive(handshake)时间间隔?

  • keep-alive
  • mqtt
已采纳

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条回答

  • dongzhong5573 dongzhong5573 7年前

    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)
      }
      ...
    
    点赞 评论 复制链接分享