|
1 | 1 | package websockets |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "time" |
| 5 | + |
4 | 6 | "github.com/gorilla/websocket" |
5 | 7 | ) |
6 | 8 |
|
7 | | -// We wrap gorilla's websocket connection with interface |
8 | | -// to be able to mock it in order to test the types dependent on it |
9 | | - |
10 | 9 | type WebsocketConnection interface { |
11 | 10 | ReadJSON(v interface{}) error |
12 | 11 | WriteJSON(v interface{}) error |
| 12 | + WriteControl(messageType int, deadline time.Time) error |
13 | 13 | Close() error |
| 14 | + SetReadDeadline(deadline time.Time) error |
| 15 | + SetWriteDeadline(deadline time.Time) error |
| 16 | + SetPongHandler(h func(string) error) |
14 | 17 | } |
15 | 18 |
|
16 | | -type GorillaWebsocketConnection struct { |
| 19 | +type WebsocketConnectionImpl struct { |
17 | 20 | conn *websocket.Conn |
18 | 21 | } |
19 | 22 |
|
20 | | -func NewGorillaWebsocketConnection(conn *websocket.Conn) *GorillaWebsocketConnection { |
21 | | - return &GorillaWebsocketConnection{ |
| 23 | +func NewWebsocketConnection(conn *websocket.Conn) *WebsocketConnectionImpl { |
| 24 | + return &WebsocketConnectionImpl{ |
22 | 25 | conn: conn, |
23 | 26 | } |
24 | 27 | } |
25 | 28 |
|
26 | | -var _ WebsocketConnection = (*GorillaWebsocketConnection)(nil) |
| 29 | +var _ WebsocketConnection = (*WebsocketConnectionImpl)(nil) |
| 30 | + |
| 31 | +func (c *WebsocketConnectionImpl) ReadJSON(v interface{}) error { |
| 32 | + return c.conn.ReadJSON(v) |
| 33 | +} |
| 34 | + |
| 35 | +func (c *WebsocketConnectionImpl) WriteJSON(v interface{}) error { |
| 36 | + return c.conn.WriteJSON(v) |
| 37 | +} |
| 38 | + |
| 39 | +func (c *WebsocketConnectionImpl) WriteControl(messageType int, deadline time.Time) error { |
| 40 | + return c.conn.WriteControl(messageType, nil, deadline) |
| 41 | +} |
27 | 42 |
|
28 | | -func (m *GorillaWebsocketConnection) ReadJSON(v interface{}) error { |
29 | | - return m.conn.ReadJSON(v) |
| 43 | +func (c *WebsocketConnectionImpl) Close() error { |
| 44 | + return c.conn.Close() |
30 | 45 | } |
31 | 46 |
|
32 | | -func (m *GorillaWebsocketConnection) WriteJSON(v interface{}) error { |
33 | | - return m.conn.WriteJSON(v) |
| 47 | +func (c *WebsocketConnectionImpl) SetReadDeadline(deadline time.Time) error { |
| 48 | + return c.conn.SetReadDeadline(deadline) |
34 | 49 | } |
35 | 50 |
|
36 | | -func (m *GorillaWebsocketConnection) SetCloseHandler(handler func(code int, text string) error) { |
37 | | - m.conn.SetCloseHandler(handler) |
| 51 | +func (c *WebsocketConnectionImpl) SetWriteDeadline(deadline time.Time) error { |
| 52 | + return c.conn.SetWriteDeadline(deadline) |
38 | 53 | } |
39 | 54 |
|
40 | | -func (m *GorillaWebsocketConnection) Close() error { |
41 | | - return m.conn.Close() |
| 55 | +func (c *WebsocketConnectionImpl) SetPongHandler(h func(string) error) { |
| 56 | + c.conn.SetPongHandler(h) |
42 | 57 | } |
0 commit comments