chirpnest/mq.go

56 lines
1.2 KiB
Go
Raw Normal View History

2019-05-03 05:58:36 +02:00
package main
import (
2019-05-03 09:19:29 +02:00
"encoding/json"
2019-05-03 05:58:36 +02:00
"log"
"github.com/streadway/amqp"
)
func MQMainReceive() {
2019-05-03 09:15:16 +02:00
var m ChatWarsMessage
2019-05-03 05:59:57 +02:00
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue)
2019-05-03 05:58:36 +02:00
failOnError(err, "MQMainReceive : Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
failOnError(err, "MQMainReceive : Failed to open a channel")
defer ch.Close()
q, err := ch.QueueDeclare(
"msg", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
failOnError(err, "MQMainReceive : Failed to declare a queue")
msgs, err := ch.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
failOnError(err, "MQMainReceive : Failed to register a consumer")
forever := make(chan bool)
go func() {
for d := range msgs {
log.Printf("MQMainReceive : Received a message: %s", d.Body)
2019-05-03 09:15:16 +02:00
if err = json.Unmarshal(d.Body, &m); err != nil {
failOnError(err, "MQMainReceive : Can't unmarshal")
}
putMsg(m)
2019-05-03 05:58:36 +02:00
}
}()
<-forever
}