-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.go
57 lines (46 loc) · 1.4 KB
/
echo.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
package echo
import (
"fmt"
"github.com/SirGFM/GoWebSocketProxy/websocket"
"math/rand"
)
type echoServer struct{
// Store the connection to the client
conn websocket.ClientConnection
// Value that identifies this connection to the client
uid string
}
// Retrieves the template for the echo server
func GetTemplate() websocket.Server {
return &echoServer{}
}
// Stores the connection (so we may echo the message) and create a unique
// identifier.
func (*echoServer) Clone(conn websocket.ClientConnection) (websocket.Server, error) {
uid := make([]byte, 8)
if _, err := rand.Read(uid); err != nil {
return nil, err
}
ret := &echoServer{
conn: conn,
uid: fmt.Sprintf("%#x - ", uid),
}
return ret, nil
}
// Echo the message back, prepending the connection identifier
func (es *echoServer) Do(msg []byte, offset int) (err error) {
var payload []byte
if string(msg[offset:]) == "close" {
err = es.conn.Close(websocket.NormalClosure, []byte(es.uid))
} else {
payload = append(payload, es.uid...)
// Skip the first 'offset' bytes, which contain the received WebSocket frame
payload = append(payload, msg[offset:]...)
// Wrap the message into a WebSocket frame
err = es.conn.Send(payload, websocket.TextFrame)
}
return err
}
// No clean-up needed
func (*echoServer) Cleanup() {
}