45 lines
976 B
Go
45 lines
976 B
Go
package apiproto
|
|
|
|
import (
|
|
context "context"
|
|
"log"
|
|
|
|
grpc "google.golang.org/grpc"
|
|
"joylink.club/bj-rtsts-server/config"
|
|
)
|
|
|
|
// Centrifugo 实时消息传递客户端
|
|
var client CentrifugoApiClient
|
|
|
|
// Centrifugo 客户端初始化
|
|
func InitClient() {
|
|
conn, err := grpc.Dial(config.Config.Messaging.Centrifugo.Address, grpc.WithInsecure())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
client = NewCentrifugoApiClient(conn)
|
|
}
|
|
|
|
// 返回 Centrifugo 客户端
|
|
func Cli() CentrifugoApiClient {
|
|
return client
|
|
}
|
|
|
|
// 发布消息
|
|
func PublishMsg(channalName string, data []byte) {
|
|
resp, err := client.Publish(context.Background(), &PublishRequest{
|
|
Channel: channalName,
|
|
Data: data,
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("Transport level error: %v \n", err)
|
|
} else {
|
|
if resp.GetError() != nil {
|
|
respError := resp.GetError()
|
|
log.Fatalf("Publish msg[%s] error %d(%s)\n", channalName, respError.Code, respError.Message)
|
|
} else {
|
|
log.Println("Successfully published")
|
|
}
|
|
}
|
|
}
|