104 lines
2.6 KiB
Go
104 lines
2.6 KiB
Go
package rabbitmq
|
|
|
|
import (
|
|
"time"
|
|
|
|
"clickandjoin.app/websocketserver/modules/config"
|
|
gorabbitmqclient "git.clickandjoin.umbach.dev/ClickandJoin/go-rabbitmq-client"
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
)
|
|
|
|
const (
|
|
exchangeWebsocketMessages = "cnj.websocketserver.messages"
|
|
exchangeBroadcastMessages = "cnj.api.broadcast.messages"
|
|
)
|
|
|
|
var (
|
|
WebsocketClient *gorabbitmqclient.Client
|
|
ApiBroadcastClient *gorabbitmqclient.Client
|
|
ConnectionAddress string
|
|
WebSocketChannelClosedChannel chan *amqp.Error // informs when the connection has been lost
|
|
)
|
|
|
|
func Init() {
|
|
cfg := config.Cfg
|
|
|
|
ConnectionAddress = gorabbitmqclient.GetConnectionString(cfg.RabbitMq.Username, cfg.RabbitMq.Password, cfg.RabbitMq.Host)
|
|
|
|
/*
|
|
* websocketserver messages
|
|
*/
|
|
WebsocketClient = gorabbitmqclient.NewClient(
|
|
cfg.Debug,
|
|
gorabbitmqclient.ExchangeSettings{
|
|
Enabled: true,
|
|
Name: exchangeWebsocketMessages,
|
|
Type: "direct",
|
|
Durable: true,
|
|
AutoDeleted: false,
|
|
Internal: false,
|
|
NoWait: false,
|
|
Arguments: nil,
|
|
ExchangePublishSettings: gorabbitmqclient.ExchangePublishSettings{
|
|
Mandatory: false,
|
|
Immediate: false,
|
|
ContentType: gorabbitmqclient.ContentTypeJson,
|
|
},
|
|
},
|
|
gorabbitmqclient.QueueSettings{},
|
|
gorabbitmqclient.ChannelQosSettingsDefault,
|
|
gorabbitmqclient.Config{
|
|
ReconnectDelay: 1 * time.Second,
|
|
ReInitDelay: 1 * time.Second,
|
|
ResendDelay: 5 * time.Second,
|
|
},
|
|
ConnectionAddress)
|
|
|
|
/*
|
|
* api broadcast messages
|
|
*/
|
|
|
|
ApiBroadcastClient = gorabbitmqclient.NewClient(
|
|
cfg.Debug,
|
|
gorabbitmqclient.ExchangeSettings{},
|
|
gorabbitmqclient.QueueSettings{
|
|
Enabled: true,
|
|
Name: "",
|
|
Durable: false,
|
|
DeleteWhenUnused: false,
|
|
Exclusive: true,
|
|
NoWait: false,
|
|
Arguments: nil,
|
|
QueueBindSettings: gorabbitmqclient.QueueBindSettings{
|
|
Enabled: true,
|
|
QueueName: gorabbitmqclient.SetQueueNameToAutomaticallyAssignedQueueName,
|
|
RoutingKey: "",
|
|
Exchange: exchangeBroadcastMessages,
|
|
NoWait: false,
|
|
Arguments: nil,
|
|
},
|
|
},
|
|
gorabbitmqclient.ChannelQosSettingsDefault,
|
|
gorabbitmqclient.Config{
|
|
ReconnectDelay: 1 * time.Second,
|
|
ReInitDelay: 1 * time.Second,
|
|
ResendDelay: 5 * time.Second,
|
|
},
|
|
ConnectionAddress)
|
|
|
|
// give the connection sometime to setup
|
|
<-time.After(time.Second)
|
|
|
|
/*
|
|
* websocketserver messages
|
|
*/
|
|
WebSocketChannelClosedChannel = make(chan *amqp.Error, 1)
|
|
WebsocketClient.Channel.NotifyClose(WebSocketChannelClosedChannel)
|
|
|
|
/*
|
|
* api broadcast messages
|
|
*/
|
|
|
|
go ApiBroadcastMessagesHandling()
|
|
}
|