-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnamespace.go
52 lines (43 loc) · 1017 Bytes
/
namespace.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
package socketio
import "sync"
// Namespace is the name space of socket.io handler.
type Namespace interface {
// Name returns the name of namespace.
Name() string
// Of returns the namespace with given name.
Of(name string) Namespace
// On registers the function f to handle message.
On(message string, f interface{}) error
}
type namespace struct {
*baseHandler
root map[string]Namespace
lock sync.RWMutex
}
func newNamespace(broadcast BroadcastAdaptor) *namespace {
ret := &namespace{
baseHandler: newBaseHandler("", broadcast),
root: make(map[string]Namespace),
}
ret.root[ret.Name()] = ret
return ret
}
func (n *namespace) Name() string {
return n.baseHandler.name
}
func (n *namespace) Of(name string) Namespace {
if name == "/" {
name = ""
}
n.lock.Lock()
defer n.lock.Unlock()
if ret, ok := n.root[name]; ok {
return ret
}
ret := &namespace{
baseHandler: newBaseHandler(name, n.baseHandler.broadcast),
root: n.root,
}
n.root[name] = ret
return ret
}