-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathemitter.go
198 lines (168 loc) · 3.55 KB
/
emitter.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package emitter
import (
"bytes"
"fmt"
"gopkg.in/redis.v5"
"gopkg.in/vmihailenco/msgpack.v2"
)
const (
// https://github.com/socketio/socket.io-parser/blob/master/index.js
gEvent = 2
gBinaryEvent = 5
uid = "emitter"
)
// Options ...
type Options struct {
// host to connect to redis on (localhost)
Host string
// port to connect to redis on (6379)
Port int
// password to connect to redis
Password string
// DB
DB int
// the name of the key to pub/sub events on as prefix (socket.io)
Key string
// unix domain socket to connect to redis on ("/tmp/redis.sock")
Socket string
// redis client
Redis *redis.Client
}
// Emitter Socket.IO redis base emitter
type Emitter struct {
redis *redis.Client
prefix string
rooms []string
flags map[string]interface{}
}
// NewEmitter Emitter constructor
func NewEmitter(opts *Options) *Emitter {
emitter := &Emitter{}
if opts.Redis != nil {
emitter.redis = opts.Redis
} else {
host := "127.0.0.1"
if opts.Host != "" {
host = opts.Host
}
port := 6379
if opts.Port > 0 && opts.Port < 65536 {
port = opts.Port
}
redisURI := fmt.Sprintf("%s:%d", host, port)
emitter.redis = redis.NewClient(&redis.Options{
Addr: redisURI,
Password: opts.Password,
DB: opts.DB,
})
}
emitter.prefix = "socket.io"
if opts.Key != "" {
emitter.prefix = opts.Key
}
emitter.rooms = make([]string, 0, 0)
emitter.flags = make(map[string]interface{})
return emitter
}
// Close release redis client
func (e *Emitter) Close() {
if e.redis != nil {
e.redis.Close()
}
}
// Emit Send the packet
func (e *Emitter) Emit(data ...interface{}) (*Emitter, error) {
packet := make(map[string]interface{})
packet["type"] = gEvent
if hasBin(data...) {
packet["type"] = gBinaryEvent
}
packet["data"] = data
packet["nsp"] = "/"
if nsp, ok := e.flags["nsp"]; ok {
packet["nsp"] = nsp
delete(e.flags, "nsp")
}
opts := map[string]interface{}{
"rooms": e.rooms,
"flags": e.flags,
}
chn := fmt.Sprintf("%s#%s#", e.prefix, packet["nsp"])
buf, err := msgpack.Marshal([]interface{}{uid, packet, opts})
if err != nil {
return nil, err
}
if len(e.rooms) > 0 {
for _, room := range e.rooms {
chnRoom := fmt.Sprintf("%s%s#", chn, room)
e.redis.Publish(chnRoom, string(buf))
}
} else {
e.redis.Publish(chn, string(buf))
}
e.rooms = make([]string, 0, 0)
e.flags = make(map[string]interface{})
return e, nil
}
// In Limit emission to a certain `room`
func (e *Emitter) In(room string) *Emitter {
for _, r := range e.rooms {
if r == room {
return e
}
}
e.rooms = append(e.rooms, room)
return e
}
// To Limit emission to a certain `room`
func (e *Emitter) To(room string) *Emitter {
return e.In(room)
}
// Of Limit emission to certain `namespace`
func (e *Emitter) Of(namespace string) *Emitter {
e.flags["nsp"] = namespace
return e
}
// JSON flag
func (e *Emitter) JSON() *Emitter {
e.flags["json"] = true
return e
}
// Volatile flag
func (e *Emitter) Volatile() *Emitter {
e.flags["volatile"] = true
return e
}
// Broadcast flag
func (e *Emitter) Broadcast() *Emitter {
e.flags["broadcast"] = true
return e
}
func hasBin(data ...interface{}) bool {
if data == nil {
return false
}
for _, d := range data {
switch res := d.(type) {
case []byte:
return true
case bytes.Buffer:
return true
case []interface{}:
for _, each := range res {
if hasBin(each) {
return true
}
}
case map[string]interface{}:
for _, val := range res {
if hasBin(val) {
return true
}
}
default:
return false
}
}
return false
}