-
Notifications
You must be signed in to change notification settings - Fork 6
/
serve.go
90 lines (74 loc) · 1.72 KB
/
serve.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
package gws
import (
"net/http"
"github.com/gorilla/websocket"
)
const (
serverDefaultWSPath = "/ws"
serverDefaultAddr = ":9501"
)
var defaultUpgrade = &websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(*http.Request) bool {
return true
},
}
// Server 定义运行 websocket 服务 所需的参数
type Server struct {
// 定义服务监听端口
Addr string
// 定义 websocket 服务的路由 , 默认 "/ws".
WSPath string
// websocket 的升级主要通过 以下 包来实现
// "github.com/gorilla/websocket".
//
// 默认的 upgrader ReadBufferSize 和 WriteBufferSize 都是 1024 CheckOrigin 方法 为不检查
Upgrader *websocket.Upgrader
wh *websocketHandler
// 连接事件
OnOpen func(conn *Conn, fd int)
// 消息接受事件
OnMessage func(conn *Conn, fd int, message string, err error)
// 连接关闭事件
OnClose func(conn *Conn, fd int)
OnHttp []*HttpHandler
}
// ListenAndServe 监听tcp 连接并处理 websocket 请求
func (s *Server) ListenAndServe() error {
b := &binder{
userID2ConnMap: make(map[int]*Conn),
}
// websocket 请求处理结构体
wh := websocketHandler{
upgrader: defaultUpgrade,
binder: b,
}
if s.OnClose != nil {
wh.onClose = s.OnClose
}
if s.OnMessage != nil {
wh.onMessage = s.OnMessage
}
if s.OnOpen != nil {
wh.onOpen = s.OnOpen
}
if s.Upgrader != nil {
wh.upgrader = s.Upgrader
}
if s.WSPath == "" {
s.WSPath = serverDefaultWSPath
}
if s.Addr == "" {
s.Addr = serverDefaultAddr
}
s.wh = &wh
http.Handle(s.WSPath, s.wh)
// 新增 http 请求的处理
if len(s.OnHttp) > 0 {
for _, v := range s.OnHttp {
http.Handle(v.Path, v.DealFunc)
}
}
return http.ListenAndServe(s.Addr, nil)
}