-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (52 loc) · 1.17 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
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/gorilla/websocket"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("wss URL is required as only arg")
return
}
url := os.Args[1]
conn, resp, err := websocket.DefaultDialer.Dial(url, http.Header{})
if err != nil {
fmt.Printf("Error establishing WebSocket connection: %s\n", err)
if resp != nil {
fmt.Printf("Response received\n")
fmt.Printf("Status Code: %d\n", resp.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
if err == nil {
fmt.Printf("Body: %s\n", body)
}
fmt.Printf("Headers:\n")
for key, values := range resp.Header {
fmt.Printf(" %s: %s\n", key, strings.Join(values, ", "))
}
}
return
}
defer conn.Close()
fmt.Printf("Connected to %s\n", url)
go func() {
reader := bufio.NewReader(os.Stdin)
for {
text, _ := reader.ReadBytes('\n')
text = bytes.Replace(text, []byte{'\n'}, []byte{}, -1)
conn.WriteMessage(websocket.TextMessage, text)
}
}()
for {
_, msg, err := conn.ReadMessage()
if err != nil {
fmt.Printf("Error reading message from WebSocket: %s\n", err)
}
fmt.Printf("< %s\n", msg)
}
}