forked from pufferpanel/pufferpanel
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocket.go
More file actions
38 lines (31 loc) · 685 Bytes
/
socket.go
File metadata and controls
38 lines (31 loc) · 685 Bytes
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
package pufferpanel
import (
"encoding/json"
"github.com/gorilla/websocket"
"sync"
)
func Create(ws *websocket.Conn) *Socket {
return &Socket{conn: ws}
}
type Socket struct {
conn *websocket.Conn
locker sync.Mutex
}
func (s *Socket) WriteMessage(data []byte) error {
s.locker.Lock()
defer s.locker.Unlock()
return s.conn.WriteMessage(websocket.TextMessage, data)
}
func (s *Socket) WriteJSON(data interface{}) error {
d, err := json.Marshal(data)
if err != nil {
return err
}
return s.WriteMessage(d)
}
func (s *Socket) Close() error {
return s.conn.Close()
}
func (s *Socket) ReadMessage() (messageType int, p []byte, err error) {
return s.conn.ReadMessage()
}