-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
165 lines (139 loc) · 3.85 KB
/
handler.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package pusher
import (
"encoding/json"
"errors"
"github.com/FlowerWrong/pusher/api/forms"
"github.com/FlowerWrong/pusher/log"
)
// ParsePayload ...
func (s *Session) ParsePayload(payloadBytes []byte) error {
var requestEvent RequestEvent
err := json.Unmarshal(payloadBytes, &requestEvent)
if err != nil {
return err
}
switch requestEvent.Event {
case PusherSubscribe:
return s.subscribeHandler(payloadBytes)
case PusherUnsubscribe:
return s.unsubscribeHandler(payloadBytes)
case PusherPing:
s.Send(PingPongPack(PusherPong))
case PusherPong:
s.resetReadTimeout()
default:
if IsClientEvent(requestEvent.Event) {
return s.clientEventHandler(payloadBytes)
}
log.Error("Unsupported event name", requestEvent.Event)
}
return nil
}
// @doc https://pusher.com/docs/channels/server_api/excluding-event-recipients
// TODO rate limit https://pusher.com/docs/channels/using_channels/events#rate-limit-your-events
func (s *Session) clientEventHandler(payloadBytes []byte) error {
var clientChannelEvent ClientChannelEvent
err := json.Unmarshal(payloadBytes, &clientChannelEvent)
if err != nil {
return err
}
channel := clientChannelEvent.Channel
if !s.subscriptions[channel] {
return errors.New("you must subscribed to this channel")
}
if !IsPresenceChannel(channel) && !IsPrivateChannel(channel) {
return errors.New("client event only support private or presence channel")
}
var eventForm forms.EventForm
eventForm.Channel = channel
eventForm.Name = clientChannelEvent.Event
eventForm.SocketID = s.socketID // exclude
dataBytes, err := clientChannelEvent.Data.MarshalJSON()
if err != nil {
return err
}
eventForm.Data = string(dataBytes[:])
hookEvent := HookEvent{Name: "client_event", Channel: channel, Event: clientChannelEvent.Event, Data: string(dataBytes[:]), SocketID: s.socketID}
if IsPresenceChannel(channel) {
presenceChannelData, err := GetPresenceChannelData(s.socketID, channel)
if err != nil {
return err
}
hookEvent.UserID = presenceChannelData.UserID
eventForm.UserID = presenceChannelData.UserID
}
err = PublishEventForm(eventForm)
if err != nil {
return err
}
go TriggerHook(&hookEvent)
return nil
}
func (s *Session) unsubscribeHandler(payloadBytes []byte) error {
var unsubscribe Unsubscribe
err := json.Unmarshal(payloadBytes, &unsubscribe)
if err != nil {
return err
}
channel := unsubscribe.Data.Channel
if !ValidChannel(channel) {
return ErrInvalidChannel
}
if !s.subscriptions[channel] {
return errors.New("not subscribed")
}
err = s.unsubscribe(channel)
if err != nil {
return err
}
return nil
}
func (s *Session) subscribeHandler(payloadBytes []byte) error {
var subscribe Subscribe
err := json.Unmarshal(payloadBytes, &subscribe)
if err != nil {
return err
}
channel := subscribe.Data.Channel
if !ValidChannel(channel) {
return ErrInvalidChannel
}
if s.subscriptions[channel] {
return errors.New("have subscribed")
}
if IsPresenceChannel(channel) {
if ChannelSubscriberCount(channel) >= PresenceMaximumMembersCount {
return ErrPresenceSubscriberTooMuch
}
userData := subscribe.Data.ChannelData
if len(userData) > PresenceMaximumUserDataSize {
return ErrPresenceUserDataTooMuch
}
var presenceChannelData PresenceChannelData
err := json.Unmarshal([]byte(userData), &presenceChannelData)
if err != nil {
return err
}
if len(presenceChannelData.UserID) > PresenceMaximumUserIDLen {
return ErrPresenceUserIDTooLong
}
err = SetUserInfo(s.socketID, map[string]interface{}{
UserInfoChannelDataKey(channel): userData,
UserInfoAuthKey(channel): subscribe.Data.Auth,
})
if err != nil {
return err
}
}
if IsPrivateChannel(channel) {
err = SetUserInfoField(s.socketID, UserInfoAuthKey(channel), subscribe.Data.Auth)
if err != nil {
return err
}
}
err = s.subscribe(channel)
if err != nil {
return err
}
return nil
}