forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
175 lines (142 loc) · 4.54 KB
/
server.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
package socketio
import (
"net/http"
"github.com/thisismz/go-socket.io/v4/engineio"
)
// Server is a go-socket.io server.
type Server struct {
engine *engineio.Server
nspHandlers *Handlers
redisAdapter *RedisAdapterConfig
}
// NewServer returns a server.
func NewServer(opts *engineio.Options) *Server {
return &Server{
nspHandlers: NewHandlers(),
engine: engineio.NewServer(opts),
}
}
// Adapter sets redis broadcast adapter.
func (s *Server) Adapter(opts *RedisAdapterConfig) (bool, error) {
s.redisAdapter = GetOptions(opts)
return true, nil
}
// Close closes server.
func (s *Server) Close() error {
return s.engine.Close()
}
// ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.engine.ServeHTTP(w, r)
}
// OnConnect set a handler function f to handle open event for
func (s *Server) OnConnect(namespace string, f OnConnectHandler) {
h := s.getOrCreateNamespaceHandler(namespace)
h.OnConnect(f)
}
// OnDisconnect set a handler function f to handle disconnect event for
func (s *Server) OnDisconnect(namespace string, f OnDisconnectHandler) {
h := s.getOrCreateNamespaceHandler(namespace)
h.OnDisconnect(f)
}
// OnError set a handler function f to handle error for
func (s *Server) OnError(namespace string, f OnErrorHandler) {
h := s.getOrCreateNamespaceHandler(namespace)
h.OnError(f)
}
// OnEvent set a handler function f to handle event for
func (s *Server) OnEvent(namespace string, event string, f interface{}) {
h := s.getOrCreateNamespaceHandler(namespace)
h.OnEvent(event, f)
}
// Serve serves go-socket.io server.
func (s *Server) Serve() error {
for {
conn, err := s.engine.Accept()
//todo maybe need check EOF from Accept()
if err != nil {
return err
}
go func(conn engineio.Conn) {
defer func() {
s.engine.Remove(conn.ID())
}()
c := NewConn(conn, s.nspHandlers)
c.Serve()
}(conn)
}
}
// JoinRoom joins given connection to the room.
func (s *Server) JoinRoom(namespace string, room string, conn Conn) bool {
nspHandler := s.getNamespaceHandler(namespace)
return nspHandler.Join(room, conn)
}
// LeaveRoom leaves given connection from the room.
func (s *Server) LeaveRoom(namespace string, room string, conn Conn) bool {
nspHandler := s.getNamespaceHandler(namespace)
return nspHandler.Leave(room, conn)
}
// LeaveAllRooms leaves the given connection from all rooms.
func (s *Server) LeaveAllRooms(namespace string, conn Conn) bool {
nspHandler := s.getNamespaceHandler(namespace)
return nspHandler.LeaveAll(conn)
}
// ClearRoom clears the room.
func (s *Server) ClearRoom(namespace string, room string) bool {
nspHandler := s.getNamespaceHandler(namespace)
return nspHandler.Clear(room)
}
// BroadcastToRoom broadcasts given event & args to all the connections in the room.
func (s *Server) BroadcastToRoom(namespace string, room, event string, args ...interface{}) bool {
nspHandler := s.getNamespaceHandler(namespace)
return nspHandler.Send(room, event, args...)
}
// BroadcastToNamespace broadcasts given event & args to all the connections in the same
func (s *Server) BroadcastToNamespace(namespace string, event string, args ...interface{}) bool {
nspHandler := s.getNamespaceHandler(namespace)
return nspHandler.SendAll(event, args...)
}
// RoomLen gives number of connections in the room.
func (s *Server) RoomLen(namespace string, room string) int {
nspHandler := s.getNamespaceHandler(namespace)
return nspHandler.Len(room)
}
// Rooms gives list of all the rooms.
func (s *Server) Rooms(namespace string) []string {
nspHandler := s.getNamespaceHandler(namespace)
return nspHandler.Rooms(nil)
}
// ForEach sends data by DataFunc, if room does not exit sends anything.
func (s *Server) ForEach(namespace string, room string, f EachFunc) bool {
nspHandler := s.getNamespaceHandler(namespace)
return nspHandler.ForEach(room, f)
}
// Count number of connections.
func (s *Server) Count() int {
return s.engine.Count()
}
func (s *Server) getOrCreateNamespaceHandler(nsp string) *Handler {
h := s.getNamespaceHandler(nsp)
if h == nil {
h = s.createNamespaceHandler(nsp)
}
return h
}
func (s *Server) createNamespaceHandler(nsp string) *Handler {
if nsp == aliasRootNamespace {
nsp = rootNamespace
}
handler := NewHandler(nsp, s.redisAdapter)
s.nspHandlers.Set(nsp, handler)
return handler
}
func (s *Server) getNamespaceHandler(nsp string) *Handler {
if nsp == aliasRootNamespace {
nsp = rootNamespace
}
ret, ok := s.nspHandlers.Get(nsp)
if !ok {
return nil
}
return ret
}