客户端(安卓跟IOS)用mqtt协议订阅消息,然后管理后台用amqp来发送消息,但是发现用ampq协议来发送西消息,mqtt端接收不到,如果用mqtt来发送是没有问题的,代码就是用官方的测试代码如下:
// 发送端代码
conn, err := amqp.Dial("amqp://guest:guest@:5672/")
failOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch.Close()
err = ch.ExchangeDeclare(
"amq.topic", // name
"topic", // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
failOnError(err, "Failed to declare an exchange")
body := bodyFrom(os.Args)
routingKey := severityFrom(os.Args)
fmt.Println(routingKey)
err = ch.Publish(
"amq.topic", // exchange
routingKey, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
})
failOnError(err, "Failed to publish a message")
log.Printf(" [x] Sent %s", body)
}
// 客户端订阅接收代码
client := connect("sub", host)
client.Subscribe("/bin/#", 0, func(client mqtt.Client, msg mqtt.Message) {
fmt.Printf("* [%s] >> %s
", msg.Topic(), string(msg.Payload()))
})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…