dounaidu0204 2014-09-19 20:42
浏览 660
已采纳

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

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 2014-09-19 21:02
    关注

    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)
      }
      ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?