MQTT将客户端从使用cleanstate = true更改为cleanstate = false

时间:2015-12-02 08:45:30

标签: mqtt paho

我有一个客户端使用cleansession=true与我的MQTT服务器(RabbitMQ)创建了一个会话并订阅了一个主题。当我将代码更改为cleansession=false并重新启动客户端时,它遇到了无法连接的问题(使用Paho在Golang上)。但是,当我使用不同的clientId创建新会话时,一切正常。我可以重新创建此错误,只有在我尝试更改clientId的cleansession值时才会出错。

我没有在MQTT文档中看到任何内容,说客户端在后续连接期间无法更改其cleanState值。有这样的问题吗?

我的客户代码:     包主要

import (
    "fmt"
    mqtt "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
)

var mqttFunc mqtt.MessageHandler = func(client *mqtt.Client, msg mqtt.Message)       {
    fmt.Printf("Topic: %s\n", msg.Topic())
    fmt.Printf("Msg: %s\n", msg.Payload())
}

func main() {
    opts := mqtt.NewClientOptions().AddBroker("tcp://localhost:1883/")
    opts.SetClientID("clientE")
    opts.SetDefaultPublishHandler(mqttFunc)

    // This is the line I change
    opts.SetCleanSession(false)

    c := mqtt.NewClient(opts)
    token := c.Connect()
    if token.Wait() && token.Error() != nil {
        panic(token.Error())
    }

    token = c.Subscribe("together/example1", 1, nil)
    if token.Wait() && token.Error() != nil {
        fmt.Println(token.Error())
        panic(1)
    }

    forever := make(chan bool)
    <-forever
}

运行客户端时出错:

panic: Network Error : %!s(<nil>)

goroutine 16 [running]:
runtime.panic(0x28b700, 0xc208000dc0)
/usr/local/go/src/pkg/runtime/panic.c:279 +0xf5
main.main()
/Users/Foo/Work/Go/src/github.com/bar/rabbitmq/tutorial/mqtt_receiver.go:23 +0x149

goroutine 19 [finalizer wait]:
runtime.park(0x14bb0, 0x4910d0, 0x48fbc9)
/usr/local/go/src/pkg/runtime/proc.c:1369 +0x89
runtime.parkunlock(0x4910d0, 0x48fbc9)
/usr/local/go/src/pkg/runtime/proc.c:1385 +0x3b
runfinq()
/usr/local/go/src/pkg/runtime/mgc0.c:2644 +0xcf
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1445

goroutine 17 [syscall]:
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1445
exit status 2

0 个答案:

没有答案
相关问题