61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
|
|
"github.com/streadway/amqp"
|
|
)
|
|
|
|
func MQMainReceive() {
|
|
var m ChatWarsMessage
|
|
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue)
|
|
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)
|
|
|
|
if err = json.Unmarshal(d.Body, &m); err != nil {
|
|
logOnError(err, "MQMainReceive : Can't unmarshal")
|
|
} else {
|
|
err = putMsg(m)
|
|
if err != nil {
|
|
logOnError(err, "MQMainReceive")
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
|
|
<-forever
|
|
|
|
}
|