forked from coffeehc/httpx
/
websocket.go
79 lines (66 loc) · 1.89 KB
/
websocket.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
// websocket
package web
import (
"fmt"
"net"
"net/http"
"time"
"golang.org/x/net/websocket"
)
func checkOrigin(config *websocket.Config, req *http.Request) (err error) {
config.Origin, err = websocket.Origin(config, req)
if err == nil && config.Origin == nil {
return fmt.Errorf("null origin")
}
return err
}
type webScoketAdapter struct {
handler WebScoketHandler
}
func (this *webScoketAdapter) webScoketHandlerAdapter(request *http.Request, pathFragments map[string]string, reply *Reply) {
reply.startWebSocket()
server := websocket.Server{Handler: func(conn *websocket.Conn) {
defer func() {
conn.Close()
}()
this.handler(request, pathFragments, &WebSocketReply{conn})
}, Handshake: checkOrigin}
server.ServeHTTP(reply.w, request)
}
type WebScoketHandler func(request *http.Request, pathFragments map[string]string, reply *WebSocketReply)
type WebSocketReply struct {
conn *websocket.Conn
}
func (this *WebSocketReply) Read(msg []byte) (n int, err error) {
return this.conn.Read(msg)
}
func (this *WebSocketReply) Write(msg []byte) (n int, err error) {
return this.conn.Write(msg)
}
func (this *WebSocketReply) Close() error {
return this.conn.Close()
}
func (this *WebSocketReply) IsClientConn() bool {
return this.conn.IsClientConn()
}
func (this *WebSocketReply) IsServerConn() bool {
return this.conn.IsServerConn()
}
func (this *WebSocketReply) LocalAddr() net.Addr {
return this.conn.LocalAddr()
}
func (this *WebSocketReply) RemoteAddr() net.Addr {
return this.conn.RemoteAddr()
}
func (this *WebSocketReply) SetDeadline(t time.Time) error {
return this.conn.SetDeadline(t)
}
func (this *WebSocketReply) SetReadDeadline(t time.Time) error {
return this.conn.SetReadDeadline(t)
}
func (this *WebSocketReply) SetWriteDeadline(t time.Time) error {
return this.conn.SetWriteDeadline(t)
}
func (this *WebSocketReply) Config() *websocket.Config {
return this.conn.Config()
}