-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (112 loc) · 3.26 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/base64"
"flag"
"fmt"
"log"
"net"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
)
const (
pongTimeout = 60 * time.Second
)
var addrFlag = flag.String("addr", "ws://echo.websocket.org", "http service address")
var subProtocolFlag = flag.String("protocols", "", "Comma separated list of protocols to use")
var echoDelayFlag = flag.Uint("echoDelay", 0, "Delay before echoing back received message from server")
func main() {
flag.Parse()
var address string = *addrFlag
var subProtocols []string
var echoDelay = *echoDelayFlag
if address == "" {
log.Fatalf("Server address \"%s\" is invalid.", address)
}
if *subProtocolFlag != "" {
subProtocols = strings.Split(*subProtocolFlag, ",")
log.Printf("Using sub protocols: %s\n", strings.Join(subProtocols, ", "))
}
if echoDelay < 0 {
log.Fatalln("Echo delay cannot be negative")
}
var dialer *websocket.Dialer = &websocket.Dialer{
Subprotocols: subProtocols,
}
conn, _, err := dialer.Dial(address, nil)
if err != nil {
log.Fatalf("Unable to dial ws connection to agent %s with error %s\n", address, err)
}
respSubProtocol := conn.Subprotocol()
subProtocolMatch := false
if len(dialer.Subprotocols) > 0 {
for _, val := range dialer.Subprotocols {
if val == respSubProtocol {
subProtocolMatch = true
break
}
}
} else {
subProtocolMatch = respSubProtocol == ""
}
if !subProtocolMatch {
log.Fatalf("Subprotocol match failed. Server returned subprotocol: '%s'\n", respSubProtocol)
}
log.Println("Connection stabilized")
defer conn.Close()
conn.SetCloseHandler(func(code int, text string) error {
log.Printf("Received close message. Code: %d, Text: %s\n", code, text)
message := []byte{}
if code != websocket.CloseNoStatusReceived {
message = websocket.FormatCloseMessage(code, "")
}
conn.WriteControl(websocket.CloseMessage, message, time.Now().Add(pongTimeout))
return nil
})
conn.SetPingHandler(
func(appData string) error {
log.Printf("Received ping message: '%s'\n", appData)
err := conn.WriteControl(websocket.PongMessage, []byte(appData), time.Now().Add(pongTimeout))
if e, ok := err.(net.Error); ok && e.Temporary() {
log.Printf("Received temporary error while sending pong. %v\n", e)
return nil
}
if err == nil {
log.Println("Sent pong")
} else {
log.Println("Failed to send pong")
}
return err
},
)
var wg sync.WaitGroup
for {
msgType, rawMsg, err := conn.ReadMessage()
if err != nil {
fmt.Println("Error in reading message:", err)
break
}
switch msgType {
case websocket.TextMessage:
log.Printf("Received text message: %s\n", rawMsg)
case websocket.BinaryMessage:
log.Printf("Received binary message. Base64: %s\n", base64.StdEncoding.EncodeToString(rawMsg))
default:
log.Printf("Received unknown message type: %d.\n Base64 of message: %s\n", msgType, base64.StdEncoding.EncodeToString(rawMsg))
}
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(time.Duration(time.Second * time.Duration(echoDelay)))
err = conn.WriteMessage(msgType, rawMsg)
if err == nil {
log.Printf("Sent received message back to server")
} else {
log.Printf("Unable to send message to server. Error: %s\n", err)
}
}()
}
wg.Wait()
log.Println("Exiting...")
}