forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
162 lines (130 loc) · 3.36 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
package engineio
import (
"context"
"io"
"log"
"net"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/googollee/go-socket.io/engineio/session"
"github.com/googollee/go-socket.io/engineio/transport"
)
// Server is instance of server
type Server struct {
pingInterval time.Duration
pingTimeout time.Duration
transports *transport.Manager
sessions *session.Manager
requestChecker CheckerFunc
connInitor ConnInitorFunc
connChan chan Conn
closeOnce sync.Once
}
// NewServer returns a server.
func NewServer(opts *Options) *Server {
return &Server{
transports: transport.NewManager(opts.getTransport()),
pingInterval: opts.getPingInterval(),
pingTimeout: opts.getPingTimeout(),
requestChecker: opts.getRequestChecker(),
connInitor: opts.getConnInitor(),
sessions: session.NewManager(opts.getSessionIDGenerator()),
connChan: make(chan Conn, 1),
}
}
// Close closes server.
func (s *Server) Close() error {
s.closeOnce.Do(func() {
close(s.connChan)
})
return nil
}
// Accept accepts a connection.
func (s *Server) Accept() (Conn, error) {
c := <-s.connChan
if c == nil {
return nil, io.EOF
}
return c, nil
}
func (s *Server) Addr() net.Addr {
return nil
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
sid := query.Get("sid")
reqSession := s.sessions.Get(sid)
reqTransport := query.Get("transport")
srvTransport := s.transports.Get(reqTransport)
if srvTransport == nil {
http.Error(w, "invalid transport", http.StatusBadRequest)
return
}
header, err := s.requestChecker(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
for k, v := range header {
w.Header()[k] = v
}
if reqSession == nil {
if sid != "" {
http.Error(w, "invalid sid", http.StatusBadRequest)
return
}
transportConn, err := srvTransport.Accept(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
reqSession, err = s.newSession(r.Context(), transportConn, reqTransport)
if err != nil {
http.Error(w, "create new session", http.StatusBadRequest)
return
}
s.connInitor(r, reqSession)
}
if reqSession.Transport() != reqTransport {
transportConn, err := srvTransport.Accept(w, r)
if err != nil {
// don't call http.Error() for HandshakeErrors because
// they get handled by the websocket library internally.
if _, ok := err.(websocket.HandshakeError); !ok {
http.Error(w, err.Error(), http.StatusBadGateway)
}
return
}
reqSession.Upgrade(reqTransport, transportConn)
if handler, ok := transportConn.(http.Handler); ok {
handler.ServeHTTP(w, r)
}
return
}
reqSession.ServeHTTP(w, r)
}
// Count counts connected
func (s *Server) Count() int {
return s.sessions.Count()
}
func (s *Server) newSession(ctx context.Context, conn transport.Conn, reqTransport string) (*session.Session, error) {
params := transport.ConnParameters{
PingInterval: s.pingInterval,
PingTimeout: s.pingTimeout,
Upgrades: s.transports.UpgradeFrom(reqTransport),
}
newSession, err := session.New(conn, s.sessions.NewID(), reqTransport, params)
if err != nil {
return nil, err
}
s.sessions.Add(newSession)
go func() {
if err := newSession.InitSession(); err != nil {
log.Println("init new session", err)
}
s.connChan <- newSession
}()
return newSession, nil
}