-
Notifications
You must be signed in to change notification settings - Fork 2
/
conn.go
53 lines (40 loc) · 962 Bytes
/
conn.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
package main
//
// The basic websocket connection code
//
import (
"net/http"
"net/url"
"github.com/gorilla/websocket"
)
type Conn struct {
ws *websocket.Conn
}
func (c *Conn) Dial(addr *string) (err error) {
u := url.URL{Scheme: "ws", Host: *addr, Path: "/sc2api"}
originURL := u
originURL.Scheme = "http"
origin := originURL.String()
headers := make(http.Header)
headers.Add("Origin", origin)
c.ws, _, err = websocket.DefaultDialer.Dial(u.String(), headers)
if err != nil {
return err
}
return nil
}
func (c *Conn) Read() (message []byte, err error) {
_, message, err = c.ws.ReadMessage()
if err != nil {
return nil, err
}
return message, nil
}
func (c *Conn) Write(data []byte) (err error) {
err = c.ws.WriteMessage(websocket.TextMessage, data)
return err
}
func (c *Conn) Close() (err error) {
err = c.ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
return err
}