rts-sim-testing-service/grpcproto/client.go
walker 47e3b5c6f7 升级go版本到1.21
修改日志库使用slog
修改相关打印日志调用
2023-10-12 10:10:23 +08:00

47 lines
1.1 KiB
Go

package apiproto
import (
context "context"
"log/slog"
grpc "google.golang.org/grpc"
"joylink.club/bj-rtsts-server/config"
"joylink.club/bj-rtsts-server/dto"
)
// Centrifugo 实时消息传递客户端
var client CentrifugoApiClient
// Centrifugo 客户端初始化
func InitClient() {
conn, err := grpc.Dial(config.Config.Messaging.Centrifugo.Address, grpc.WithInsecure())
if err != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
}
client = NewCentrifugoApiClient(conn)
}
// 返回 Centrifugo 客户端
func Cli() CentrifugoApiClient {
return client
}
// 发布消息
func PublishMsg(channalName string, data []byte) {
if len(data) == 0 {
return
}
resp, err := client.Publish(context.Background(), &PublishRequest{
Channel: channalName,
Data: data,
})
if err != nil {
slog.Error("Transport level error", "error", err)
} else {
if resp.GetError() != nil {
respError := resp.GetError()
slog.Error("Publish msg error ", "channalName", channalName, "errCode", respError.Code, "errMsg", respError.Message)
}
}
}