forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
142 lines (120 loc) · 3.27 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
package socketio
import (
"net/http"
engineio "github.com/googollee/go-engine.io"
)
// Server is a go-socket.io server.
type Server struct {
broadcast Broadcast
handlers map[string]*namespaceHandler
eio *engineio.Server
}
// NewServer returns a server.
func NewServer(c *engineio.Options) (*Server, error) {
eio, err := engineio.NewServer(c)
if err != nil {
return nil, err
}
return &Server{
handlers: make(map[string]*namespaceHandler),
eio: eio,
broadcast: NewBroadcast(),
}, nil
}
// Close closes server.
func (s *Server) Close() error {
return s.eio.Close()
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.eio.ServeHTTP(w, r)
}
// OnConnect set a handler function f to handle open event for
// namespace nsp.
func (s *Server) OnConnect(nsp string, f func(Conn) error) {
h := s.getNamespace(nsp)
h.OnConnect(f)
}
// OnDisconnect set a handler function f to handle disconnect event for
// namespace nsp.
func (s *Server) OnDisconnect(nsp string, f func(Conn, string)) {
h := s.getNamespace(nsp)
h.OnDisconnect(f)
}
// OnError set a handler function f to handle error for namespace nsp.
func (s *Server) OnError(nsp string, f func(error)) {
h := s.getNamespace(nsp)
h.OnError(f)
}
// OnEvent set a handler function f to handle event for namespace nsp.
func (s *Server) OnEvent(nsp, event string, f interface{}) {
h := s.getNamespace(nsp)
h.OnEvent(event, f)
}
// Serve serves go-socket.io server
func (s *Server) Serve() error {
for {
conn, err := s.eio.Accept()
if err != nil {
return err
}
go s.serveConn(conn)
}
}
// JoinRoom joins given connection to the room
func (s *Server) JoinRoom(room string, connection Conn) {
s.broadcast.Join(room, connection)
}
// LeaveRoom leaves given connection from the room
func (s *Server) LeaveRoom(room string, connection Conn) {
s.broadcast.Leave(room, connection)
}
// LeaveAllRooms leaves the given connection from all rooms
func (s *Server) LeaveAllRooms(connection Conn) {
s.broadcast.LeaveAll(connection)
}
// ClearRoom clears the room
func (s *Server) ClearRoom(room string) {
s.broadcast.Clear(room)
}
// BroadcastToRoom broadcasts given event & args to all the connections in the room
func (s *Server) BroadcastToRoom(room, event string, args ...interface{}) {
s.broadcast.Send(room, event, args...)
}
// RoomLen gives number of connections in the room
func (s *Server) RoomLen(room string) int {
return s.broadcast.Len(room)
}
// RoomLen gives number of connections in the room
func (s *Server) RoomConnectionIDs(room string) []string {
return s.broadcast.ConnectionIDs(room)
}
// GetConnection gives connection details of provided connectionID
func (s *Server) GetConnection(connectionID string) Conn {
return s.broadcast.Connection(connectionID)
}
// Rooms gives list of all the rooms
func (s *Server) Rooms() []string {
return s.broadcast.Rooms(nil)
}
func (s *Server) serveConn(c engineio.Conn) {
_, err := newConn(c, s.handlers, s.broadcast)
if err != nil {
root := s.handlers[""]
if root != nil && root.onError != nil {
root.onError(err)
}
return
}
}
func (s *Server) getNamespace(nsp string) *namespaceHandler {
if nsp == "/" {
nsp = ""
}
ret, ok := s.handlers[nsp]
if ok {
return ret
}
handler := newHandler()
s.handlers[nsp] = handler
return handler
}