-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_conn.go
94 lines (78 loc) · 2.04 KB
/
ws_conn.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
package main
import (
"github.com/gorilla/websocket"
"log"
"net"
"net/http"
"sync"
"time"
)
var (
// wsUpgrader is Gorilla's websocket upgrade struct with default options
wsUpgrader = websocket.Upgrader{}
)
// WSConn wraps a gorilla websocket.Conn to implement net.Conn
type WSConn struct {
conn *websocket.Conn
sendMu sync.Mutex
}
func NewWSConn(conn *websocket.Conn) *WSConn {
return &WSConn{conn: conn, sendMu: sync.Mutex{}}
}
// FromHTTP takes in a hub and manages a client websocket lifecycle
// for the connected client.
// This function will also handle any websocket specific functions not needed in Hub (Ping, Pong, etc)
func FromHTTP(hub *Hub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
conn, err := wsUpgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(r.RemoteAddr, err)
return
}
wsconn := NewWSConn(conn)
defer func() {
hub.Deregister(wsconn)
wsconn.conn.Close()
}()
hub.Register(wsconn)
for {
kind, bs, err := wsconn.conn.ReadMessage()
if err != nil {
log.Println(wsconn.conn.RemoteAddr(), err)
break
}
if kind == websocket.TextMessage {
hub.OnMessage(string(bs))
}
}
}
}
// Read should not be called by the Hub. All reads are handled asynchronously
func (w *WSConn) Read(b []byte) (n int, err error) {
panic("not used")
}
// Write sends data to the connected client. Write is protected by a mutex as websocket.Conn writes
// are not thread-safe.
func (w *WSConn) Write(b []byte) (n int, err error) {
w.sendMu.Lock()
defer w.sendMu.Unlock()
return len(b), w.conn.WriteMessage(websocket.TextMessage, b)
}
func (w *WSConn) Close() error {
return w.conn.Close()
}
func (w *WSConn) LocalAddr() net.Addr {
return w.conn.LocalAddr()
}
func (w *WSConn) RemoteAddr() net.Addr {
return w.conn.RemoteAddr()
}
func (w *WSConn) SetDeadline(t time.Time) error {
return nil
}
func (w *WSConn) SetReadDeadline(t time.Time) error {
return w.conn.SetReadDeadline(t)
}
func (w *WSConn) SetWriteDeadline(t time.Time) error {
return w.conn.SetWriteDeadline(t)
}