2014-09-19 262 views

回答

4

keepAlive'握手'是必需的,它不能被禁用,它是經紀人如何知道客戶端仍然連接。

在將opts對象傳遞給NewClient方法之前,您可以通過調用SetKeepAlive來更改保持活動超時。

該方法以每秒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) 
    } 
    ... 
+0

我想你會想'opts.SetKeepAlive(30 * time.Second)'因爲它需要一個'time.Duration',(裸30可能會被迫30納秒,這本身本身可能是客戶端 - 或服務器端解釋爲「從不」或「默認」或「最小值」,如5秒鐘或其他)。這是一個古老的答案,所以也許界面可能會從秒數左右變成'time.Duration'。 – 2016-05-09 15:00:22

+0

@hardillb您可以通過將keepAlive握手設置爲零來禁用握手,請參見:http://www.hivemq.com/blog/mqtt-essentials-part-10-alive-client-take-over – Yohst 2017-05-24 15:09:15

相關問題