-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
45 lines (40 loc) · 914 Bytes
/
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
package webreal
import (
"github.com/gorilla/websocket"
"net/http"
"sync"
)
type Server struct {
hub *SubscriptionHub
handler Handler
upgrader websocket.Upgrader
conf *Config
}
func NewServer(handler Handler, hub *SubscriptionHub, c *Config) *Server {
return &Server{
hub: hub,
handler: handler,
upgrader: websocket.Upgrader{
ReadBufferSize: 256,
WriteBufferSize: 256,
WriteBufferPool: &sync.Pool{},
CheckOrigin: func(r *http.Request) bool {
return true
},
},
conf: c,
}
}
// 实现http.Handler接口
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
conn, err := s.upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
newClient(conn, s.handler, s.hub, r, s.conf).run()
}
// 使用默认的http启动监听服务
func (s *Server) Run(addr string, path string) error {
http.Handle(path, s)
return http.ListenAndServe(addr, nil)
}