/
client.go
65 lines (57 loc) · 1.59 KB
/
client.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
package main
import (
"crypto/tls"
"fmt"
"github.com/gorilla/websocket"
"log"
"net/http"
"os"
)
/* The client dials a connection to wsUri. The client starts a reader, a writer, and a standardInput reader.
* Standard in is sent to the writer that will send it on the connection. The readerResult is printed to standard out.
*/
func client(wsUri string) {
tlsConfig := tls.Config{}
tlsConfig.InsecureSkipVerify = true
dialer := websocket.Dialer{TLSClientConfig: &tlsConfig}
requestHeader := http.Header{}
requestHeader.Set("origin", "http://localhost/")
conn, _, err := dialer.Dial(wsUri, requestHeader)
if err != nil {
log.Fatal(err)
}
fmt.Printf("The gowebsock client is connected to %s\n", wsUri)
readerResultChan := make(chan readerResult)
go reader(conn, readerResultChan)
writerCommandChan := make(chan writerCommand)
go writer(conn, writerCommandChan)
stdinReaderChan := make(chan string)
go stdinReader(stdinReaderChan)
for {
select {
case stdinMessage := <-stdinReaderChan:
var messageType int
data := ""
switch stdinMessage {
case "close":
messageType = 8
case "ping":
messageType = 9
case "pong":
messageType = 10
default:
messageType = 1
data = stdinMessage
}
writerCommandChan <- writerCommand{false, messageType, []byte(data)}
case readerResult := <-readerResultChan:
if readerResult.err == nil {
output := "Server: type = " + messageTypeString(readerResult.messageType) + ", data = " + string(readerResult.data) + "\n"
fmt.Printf(output)
} else {
fmt.Printf("%s\n", readerResult.err)
os.Exit(0)
}
}
}
}