2023-07-25 10:38:49 +08:00
|
|
|
package apiproto
|
|
|
|
|
2023-07-26 17:51:32 +08:00
|
|
|
import (
|
|
|
|
context "context"
|
2023-07-25 10:38:49 +08:00
|
|
|
|
2023-07-31 08:41:42 +08:00
|
|
|
"go.uber.org/zap"
|
2023-07-26 17:51:32 +08:00
|
|
|
grpc "google.golang.org/grpc"
|
|
|
|
"joylink.club/bj-rtsts-server/config"
|
2023-08-31 10:17:38 +08:00
|
|
|
"joylink.club/bj-rtsts-server/dto"
|
2023-07-26 17:51:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Centrifugo 实时消息传递客户端
|
2023-07-25 10:38:49 +08:00
|
|
|
var client CentrifugoApiClient
|
|
|
|
|
2023-07-26 17:51:32 +08:00
|
|
|
// Centrifugo 客户端初始化
|
2023-07-25 10:38:49 +08:00
|
|
|
func InitClient() {
|
2023-07-26 17:51:32 +08:00
|
|
|
conn, err := grpc.Dial(config.Config.Messaging.Centrifugo.Address, grpc.WithInsecure())
|
2023-07-25 10:38:49 +08:00
|
|
|
if err != nil {
|
2023-08-31 10:17:38 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
|
2023-07-25 10:38:49 +08:00
|
|
|
}
|
|
|
|
client = NewCentrifugoApiClient(conn)
|
|
|
|
}
|
|
|
|
|
2023-07-26 17:51:32 +08:00
|
|
|
// 返回 Centrifugo 客户端
|
2023-07-25 10:38:49 +08:00
|
|
|
func Cli() CentrifugoApiClient {
|
|
|
|
return client
|
|
|
|
}
|
2023-07-26 17:51:32 +08:00
|
|
|
|
|
|
|
// 发布消息
|
|
|
|
func PublishMsg(channalName string, data []byte) {
|
2023-07-31 08:41:42 +08:00
|
|
|
if len(data) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-07-26 17:51:32 +08:00
|
|
|
resp, err := client.Publish(context.Background(), &PublishRequest{
|
|
|
|
Channel: channalName,
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-07-31 08:41:42 +08:00
|
|
|
zap.S().Errorf("Transport level error: %v \n", err)
|
2023-07-26 17:51:32 +08:00
|
|
|
} else {
|
|
|
|
if resp.GetError() != nil {
|
|
|
|
respError := resp.GetError()
|
2023-07-31 08:41:42 +08:00
|
|
|
zap.S().Errorf("Publish msg[%s] error %d(%s)\n", channalName, respError.Code, respError.Message)
|
2023-07-26 17:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|