Skip to content

Commit 274a7eb

Browse files
committedMay 18, 2022
add project
1 parent 16c1712 commit 274a7eb

10 files changed

+1018
-8
lines changed
 

‎.gitignore

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
# Binaries for programs and plugins
2-
*.exe
3-
*.exe~
4-
*.dll
5-
*.so
6-
*.dylib
2+
**/*.exe
3+
**/*.exe~
4+
**/*.dll
5+
**/*.so
6+
**/*.dylib
77

88
# Test binary, built with `go test -c`
9-
*.test
9+
**/*.test
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
12-
*.out
12+
**/*.out
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
**/.DS_Store
18+
**/._.DS_Store
19+
20+
**/.idea
21+
**/.data

‎README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
1-
# engine.io
1+
# engine.io
2+
3+
[![GoDoc](https://godoc.org/github.com/funcards/engine.io?status.svg)](https://pkg.go.dev/github.com/funcards/engine.io)
4+
![License](https://img.shields.io/dub/l/vibe-d.svg)
5+
6+
## Installation
7+
8+
Use go get.
9+
10+
```bash
11+
go get github.com/funcards/engine.io
12+
```
13+
14+
Then import the parser package into your own code.
15+
16+
```go
17+
import "github.com/funcards/engine.io"
18+
```
19+
20+
## License
21+
22+
Distributed under MIT License, please see license file within the code for more details.

‎emitter.go

+244
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
package eio
2+
3+
import (
4+
"go.uber.org/zap"
5+
"reflect"
6+
"sync"
7+
)
8+
9+
var _ Emitter = (*emitter)(nil)
10+
11+
const (
12+
TopicOpen = "open"
13+
TopicClose = "close"
14+
TopicUpgrading = "upgrading"
15+
TopicConnect = "connect"
16+
TopicConnection = "connection"
17+
TopicDisconnect = "disconnect"
18+
TopicDisconnecting = "disconnecting"
19+
TopicHeartbeat = "heartbeat"
20+
TopicError = "error"
21+
TopicData = "data"
22+
TopicMessage = "message"
23+
TopicPacket = "packet"
24+
TopicDrain = "drain"
25+
TopicFlush = "flush"
26+
)
27+
28+
type (
29+
Event struct {
30+
Topic string
31+
Args []any
32+
}
33+
34+
Listener func(event *Event) error
35+
36+
Emitter interface {
37+
On(topic string, listeners ...Listener)
38+
Once(topic string, listeners ...Listener)
39+
OffListeners(topic string, listeners ...Listener)
40+
Off(topics ...string)
41+
Emit(topic string, args ...any) error
42+
Has(topic string) bool
43+
Listeners(topic string) []Listener
44+
}
45+
46+
emitter struct {
47+
mu sync.RWMutex
48+
listeners map[string][]Listener
49+
log *zap.Logger
50+
}
51+
)
52+
53+
func (e Event) Get(index uint, dflt ...any) (r any) {
54+
for _, n := range dflt {
55+
r = n
56+
break
57+
}
58+
if len(e.Args) > int(index) {
59+
r = e.Args[index]
60+
}
61+
return
62+
}
63+
64+
func (e Event) Int(index uint, dflt ...int) (r int) {
65+
for _, n := range dflt {
66+
r = n
67+
break
68+
}
69+
if len(e.Args) > int(index) {
70+
if n, ok := e.Args[index].(int); ok {
71+
r = n
72+
}
73+
}
74+
return
75+
}
76+
77+
func (e Event) String(index uint, dflt ...string) (r string) {
78+
for _, n := range dflt {
79+
r = n
80+
break
81+
}
82+
if len(e.Args) > int(index) {
83+
if c, ok := e.Args[index].(string); ok {
84+
r = c
85+
}
86+
}
87+
return
88+
}
89+
90+
func (e Event) Err(index uint, dflt ...error) (r error) {
91+
for _, n := range dflt {
92+
r = n
93+
break
94+
}
95+
if len(e.Args) > int(index) {
96+
if c, ok := e.Args[index].(error); ok {
97+
r = c
98+
}
99+
}
100+
return
101+
}
102+
103+
func once(emitter Emitter, topic string, listeners ...Listener) []Listener {
104+
data := make([]Listener, len(listeners))
105+
106+
for i, listener := range listeners {
107+
var fn Listener
108+
fn = func(event *Event) error {
109+
emitter.OffListeners(topic, fn)
110+
return listener(event)
111+
}
112+
data[i] = fn
113+
}
114+
return data
115+
}
116+
117+
func NewEmitter(logger *zap.Logger) *emitter {
118+
return &emitter{
119+
listeners: make(map[string][]Listener),
120+
log: logger,
121+
}
122+
}
123+
124+
func (e *emitter) On(topic string, listeners ...Listener) {
125+
if len(listeners) == 0 {
126+
return
127+
}
128+
129+
e.mu.Lock()
130+
defer e.mu.Unlock()
131+
132+
e.log.Debug("subscribe", zap.String("topic", topic))
133+
134+
if !e.has(topic) {
135+
e.listeners[topic] = listeners
136+
} else {
137+
e.listeners[topic] = append(e.listeners[topic], listeners...)
138+
}
139+
}
140+
141+
func (e *emitter) Once(topic string, listeners ...Listener) {
142+
e.log.Debug("subscribe once", zap.String("topic", topic))
143+
e.On(topic, once(e, topic, listeners...)...)
144+
}
145+
146+
func (e *emitter) OffListeners(topic string, listeners ...Listener) {
147+
e.mu.RLock()
148+
data, ok := e.listeners[topic]
149+
e.mu.RUnlock()
150+
151+
if !ok {
152+
return
153+
}
154+
155+
e.log.Debug("turn off listener for topic", zap.String("topic", topic))
156+
157+
if len(listeners) == 0 {
158+
e.mu.Lock()
159+
delete(e.listeners, topic)
160+
e.mu.Unlock()
161+
} else {
162+
ptrs := make(map[uintptr]bool, len(listeners))
163+
for _, listener := range listeners {
164+
ptrs[reflect.ValueOf(listener).Pointer()] = true
165+
}
166+
167+
newData := make([]Listener, 0)
168+
for _, listener := range data {
169+
if _, ok = ptrs[reflect.ValueOf(listener).Pointer()]; ok {
170+
continue
171+
}
172+
newData = append(newData, listener)
173+
}
174+
175+
e.mu.Lock()
176+
if len(newData) == 0 {
177+
delete(e.listeners, topic)
178+
} else {
179+
e.listeners[topic] = newData
180+
}
181+
e.mu.Unlock()
182+
}
183+
}
184+
185+
func (e *emitter) Off(topics ...string) {
186+
e.mu.Lock()
187+
defer e.mu.Unlock()
188+
189+
e.log.Debug("turn off listeners", zap.Strings("topics", topics))
190+
191+
if len(topics) == 0 {
192+
e.listeners = make(map[string][]Listener)
193+
} else {
194+
for _, topic := range topics {
195+
if e.has(topic) {
196+
delete(e.listeners, topic)
197+
}
198+
}
199+
}
200+
}
201+
202+
func (e *emitter) Emit(topic string, args ...any) error {
203+
e.mu.RLock()
204+
defer e.mu.RUnlock()
205+
206+
if !e.has(topic) {
207+
return nil
208+
}
209+
210+
e.log.Debug("emit event", zap.String("topic", topic), zap.Any("args", args))
211+
212+
event := Event{Topic: topic, Args: args}
213+
for _, listener := range e.listeners[topic] {
214+
if err := listener(&event); err != nil {
215+
return err
216+
}
217+
}
218+
return nil
219+
}
220+
221+
func (e *emitter) Has(topic string) bool {
222+
e.mu.RLock()
223+
defer e.mu.RUnlock()
224+
225+
return e.has(topic)
226+
}
227+
228+
func (e *emitter) Listeners(topic string) []Listener {
229+
e.mu.RLock()
230+
defer e.mu.RUnlock()
231+
232+
data := make([]Listener, 0)
233+
if listeners, ok := e.listeners[topic]; ok {
234+
for _, listener := range listeners {
235+
data = append(data, listener)
236+
}
237+
}
238+
return data
239+
}
240+
241+
func (e *emitter) has(topic string) bool {
242+
_, ok := e.listeners[topic]
243+
return ok
244+
}

‎go.mod

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/funcards/engine.io
2+
3+
go 1.18
4+
5+
require (
6+
github.com/funcards/engine.io-parser/v4 v4.0.0-20220513212218-27624224a21c // indirect
7+
go.uber.org/atomic v1.7.0 // indirect
8+
go.uber.org/multierr v1.6.0 // indirect
9+
go.uber.org/zap v1.21.0 // indirect
10+
)

‎go.sum

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/funcards/engine.io-parser/v4 v4.0.0-20220513212218-27624224a21c h1:GLN4isgrl0cHodpItwr/tEKyfpUFn/2sgbta9tJzaFs=
5+
github.com/funcards/engine.io-parser/v4 v4.0.0-20220513212218-27624224a21c/go.mod h1:zIu85DkDET2OrI+kX6vjeuqHHfwmqbW/kmjqmVrcpFU=
6+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
7+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
8+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
9+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
10+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
13+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
14+
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
15+
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
16+
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
17+
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
18+
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
19+
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
20+
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
21+
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
22+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
23+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
24+
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
25+
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
26+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
27+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
28+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
29+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
30+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
31+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
32+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
33+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
34+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
35+
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
36+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
37+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
38+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
39+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
40+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
41+
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
42+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
43+
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
44+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
45+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
46+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
47+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
48+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
49+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
50+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
51+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)