forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
96 lines (81 loc) · 2.71 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
package socketio
import (
"github.com/googollee/go-engine.io"
"net/http"
"time"
)
// Server is the server of socket.io.
type Server struct {
*namespace
broadcast BroadcastAdaptor
eio *engineio.Server
}
// NewServer returns the server supported given transports. If transports is nil, server will use ["polling", "websocket"] as default.
func NewServer(transportNames []string) (*Server, error) {
eio, err := engineio.NewServer(transportNames)
if err != nil {
return nil, err
}
ret := &Server{
namespace: newNamespace(newBroadcastDefault()),
eio: eio,
}
go ret.loop()
return ret, nil
}
// SetPingTimeout sets the timeout of ping. When time out, server will close connection. Default is 60s.
func (s *Server) SetPingTimeout(t time.Duration) {
s.eio.SetPingTimeout(t)
}
// SetPingInterval sets the interval of ping. Default is 25s.
func (s *Server) SetPingInterval(t time.Duration) {
s.eio.SetPingInterval(t)
}
// SetMaxConnection sets the max connetion. Default is 1000.
func (s *Server) SetMaxConnection(n int) {
s.eio.SetMaxConnection(n)
}
// SetAllowRequest sets the middleware function when establish connection. If it return non-nil, connection won't be established. Default will allow all request.
func (s *Server) SetAllowRequest(f func(*http.Request) error) {
s.eio.SetAllowRequest(f)
}
// SetAllowUpgrades sets whether server allows transport upgrade. Default is true.
func (s *Server) SetAllowUpgrades(allow bool) {
s.eio.SetAllowUpgrades(allow)
}
// SetCookie sets the name of cookie which used by engine.io. Default is "io".
func (s *Server) SetCookie(prefix string) {
s.eio.SetCookie(prefix)
}
// SetNewId sets the callback func to generate new connection id. By default, id is generated from remote addr + current time stamp
func (s *Server) SetNewId(f func(*http.Request) string) {
s.eio.SetNewId(f)
}
// SetSessionsManager sets the sessions as server's session manager. Default sessions is single process manager. You can custom it as load balance.
func (s *Server) SetSessionManager(sessions engineio.Sessions) {
s.eio.SetSessionManager(sessions)
}
// SetAdaptor sets the adaptor of broadcast. Default is in-process broadcast implement.
func (s *Server) SetAdaptor(adaptor BroadcastAdaptor) {
s.namespace = newNamespace(adaptor)
}
// ServeHTTP handles http request.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.eio.ServeHTTP(w, r)
}
// Server level broadcasts function.
func (s *Server) BroadcastTo(room, message string, args ...interface{}) {
s.namespace.BroadcastTo(room, message, args...)
}
func (s *Server) loop() {
for {
conn, err := s.eio.Accept()
if err != nil {
return
}
s := newSocket(conn, s.baseHandler)
go func(s *socket) {
s.loop()
}(s)
}
}