-
Notifications
You must be signed in to change notification settings - Fork 1
/
hub.go
43 lines (35 loc) · 884 Bytes
/
hub.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
package hub
import (
"sync"
"github.com/freeusd/solebtc/Godeps/_workspace/src/github.com/gorilla/websocket"
)
// Hub maintains active connections and broadcast messages
type Hub interface {
PutConn(Conn)
Broadcast([]byte)
Len() int
}
// Conn describes a connection's behaviour
type Conn interface {
Write([]byte) error
}
type connWrapper struct {
write func([]byte) error
mutex sync.Mutex
}
func (c *connWrapper) Write(raw []byte) error {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.write(raw)
}
func newConnWrapper(write func([]byte) error) Conn {
return &connWrapper{write: write}
}
// WrapPutWebsocketConn wraps func(Conn) to func(*websocket.Conn)
func WrapPutWebsocketConn(put func(Conn)) func(*websocket.Conn) {
return func(c *websocket.Conn) {
put(newConnWrapper(func(raw []byte) error {
return c.WriteMessage(websocket.TextMessage, raw)
}))
}
}