-
Notifications
You must be signed in to change notification settings - Fork 2
/
payload.go
136 lines (117 loc) · 3.29 KB
/
payload.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package pusher
import (
"encoding/json"
"github.com/spf13/viper"
)
const (
PusherError = "pusher:error"
PusherSubscribe = "pusher:subscribe"
PusherUnsubscribe = "pusher:unsubscribe"
PusherPing = "pusher:ping"
PusherPong = "pusher:pong"
PusherSubscriptionSucceeded = "pusher_internal:subscription_succeeded"
PusherPresenceMemberAdded = "pusher_internal:member_added"
PusherPresenceMemberRemoved = "pusher_internal:member_removed"
)
// RequestEvent ...
type RequestEvent struct {
Event string `json:"event"`
}
// Payload ...
type Payload struct {
Event string `json:"event"`
Data string `json:"data"`
}
// PayloadPack ...
func PayloadPack(event, data string) []byte {
payload := Payload{Event: event, Data: data}
payloadJSON, err := json.Marshal(payload)
if err != nil {
panic(err)
}
return payloadJSON
}
// EstablishData Channels -> Client
type EstablishData struct {
SocketID string `json:"socket_id"`
ActivityTimeout int `json:"activity_timeout"` // Protocol 7 and above
}
// EstablishPack Channels -> Client
func EstablishPack(socketID string) []byte {
data := EstablishData{SocketID: socketID, ActivityTimeout: viper.GetInt("ACTIVITY_TIMEOUT")}
b, err := json.Marshal(data)
if err != nil {
panic(err)
}
return PayloadPack("pusher:connection_established", string(b[:]))
}
// ErrData Channels -> Client
type ErrData struct {
Message string `json:"message"`
Code int `json:"code,omitempty"` // optional
}
// ErrPack Channels -> Client
func ErrPack(code int) []byte {
data := ErrData{Message: ErrCodes[code], Code: code}
b, err := json.Marshal(data)
if err != nil {
panic(err)
}
return PayloadPack(PusherError, string(b[:]))
}
// Subscribe ...
type Subscribe struct {
Event string `json:"event"`
Data SubscribeData `json:"data"`
}
// SubscribeData ...
type SubscribeData struct {
Channel string `json:"channel"`
Auth string `json:"auth,omitempty"` // optional
ChannelData string `json:"channel_data,omitempty"` // optional PresenceChannelData
}
// PingPong ...
type PingPong struct {
Event string `json:"event"`
Data interface{} `json:"data"`
}
// PingPongPack ...
func PingPongPack(event string) []byte {
data := PingPong{Event: event, Data: map[string]string{}}
b, err := json.Marshal(data)
if err != nil {
panic(err)
}
return b
}
// Unsubscribe ...
type Unsubscribe struct {
Event string `json:"event"`
Data UnsubscribeData `json:"data"`
}
// UnsubscribeData ...
type UnsubscribeData struct {
Channel string `json:"channel"`
}
// SubscriptionSucceeded CHANNELS -> CLIENT
// @doc https://pusher.com/docs/channels/library_auth_reference/pusher-websockets-protocol#presence-channel-events
type SubscriptionSucceeded struct {
Event string `json:"event"`
Channel string `json:"channel"`
Data string `json:"data"`
}
// SubscriptionSucceededPack ...
func SubscriptionSucceededPack(channel, data string) []byte {
d := SubscriptionSucceeded{Event: PusherSubscriptionSucceeded, Channel: channel, Data: data}
b, err := json.Marshal(d)
if err != nil {
panic(err)
}
return b
}
// ClientChannelEvent ...
type ClientChannelEvent struct {
Event string `json:"event"`
Data json.RawMessage `json:"data"`
Channel string `json:"channel"`
}