Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

rabbitmq 消息发布与订阅

客户端(安卓跟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()))
    })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...