/
websocketservice.go
executable file
·80 lines (59 loc) · 1.77 KB
/
websocketservice.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
package nets
import (
"context"
"errors"
"net"
"net/http"
"github.com/0x00b/gobbq/xlog"
"golang.org/x/net/websocket"
)
// curl --include --no-buffer --header "Connection: Upgrade" --header "Upgrade: websocket" --header "Host: example.com:80" --header "Origin: http://example.com:80" --header "Sec-WebSocketService-Key: SGVsbG8sIHdvcmxkIQ==" --header "Sec-WebSocketService-Version: 13" localhost:80
type WebSocketService struct {
hs http.Server
svc *service
}
func newWebSocketService(svc *service) *WebSocketService {
return &WebSocketService{
svc: svc,
}
}
func (ws *WebSocketService) ListenAndServe(network NetWorkName, address string, opts *Options) error {
if network != WebSocket {
return errors.New("not websocket")
}
xlog.Infoln("websocket listenAndServe from:", network, address)
ws.hs.Addr = address
ws.hs.SetKeepAlivesEnabled(opts.NetKeepAlive)
ws.hs.RegisterOnShutdown(func() {
})
h := websocket.Handler(func(conn *websocket.Conn) {
conn.PayloadType = websocket.BinaryFrame
ws.handleConn(conn, opts)
})
ws.hs.Handler = h
// http.Handle("/ws", h)
if opts.TLSKeyFile == "" && opts.TLSCertFile == "" {
return ws.hs.ListenAndServe()
}
return ws.hs.ListenAndServeTLS(opts.CACertFile, opts.TLSKeyFile)
}
func (ws *WebSocketService) Close(chan struct{}) error {
err := ws.hs.Shutdown(context.Background())
return err
}
func (ws *WebSocketService) Name() NetWorkName {
return WebSocket
}
func (ws *WebSocketService) handleConn(rawConn net.Conn, opts *Options) {
if ws.svc.closed.Load() {
xlog.Infoln("closed", ws.hs.Addr)
return
}
ctx, cancel := context.WithCancel(context.Background())
cn := newDefaultConn(ctx, rawConn, opts)
cn.cancel = cancel
// con err handler
cn.registerConErrHandler(ws.svc)
ws.svc.storeConn(cn)
cn.Serve()
}