-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.go
48 lines (42 loc) · 1.43 KB
/
socket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package potens
import (
"github.com/kubex/potens-go/services"
"github.com/kubex/proto-go/socket"
"go.uber.org/zap"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// Socket helper for sockets
func (app *Application) Socket() *socketHandler {
if app.services.socketHandler == nil {
con, err := app.GetServiceConnection(services.Socket().Key())
if err != nil {
app.Log().Fatal("Unable to connect to Sockets Server", zap.String("error", err.Error()))
}
app.services.socketHandler = newHandler(app.GrpcBackgroundContext(), con, app.VendorID(), app.AppID())
}
return app.services.socketHandler
}
type socketHandler struct {
client socket.SocketClient
connection *grpc.ClientConn
ctx context.Context
appID string
vendorID string
}
func newHandler(ctx context.Context, cc *grpc.ClientConn, vendor string, appID string) *socketHandler {
return &socketHandler{connection: cc, ctx: ctx, client: socket.NewSocketClient(cc), appID: appID, vendorID: vendor}
}
func (h *socketHandler) Subscribe(socketID string, channelName string) (*socket.PublishResponse, error) {
return h.client.Subscribe(h.ctx, &socket.SubscribeMessage{
SessionId: socketID,
Channel: channelName,
})
}
func (h *socketHandler) SendMessage(channelName string, action string, payload string) (*socket.PublishResponse, error) {
return h.client.Publish(h.ctx, &socket.SocketMessage{
Channel: channelName,
Action: action,
Payload: payload,
})
}