forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsocket.go
183 lines (168 loc) · 4.17 KB
/
websocket.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package slack
import (
"encoding/json"
"errors"
"io"
"log"
"net"
"net/url"
"sync"
"time"
"golang.org/x/net/websocket"
)
const (
EV_MESSAGE = iota
EV_USER_TYPING
)
type SlackWS struct {
conn *websocket.Conn
messageId int
mutex sync.Mutex
Slack
}
var portMapping = map[string]string{"ws": "80", "wss": "443"}
func fixUrlPort(orig string) (string, error) {
urlObj, err := url.ParseRequestURI(orig)
if err != nil {
return "", err
}
_, _, err = net.SplitHostPort(urlObj.Host)
if err != nil {
return urlObj.Scheme + "://" + urlObj.Host + ":" + portMapping[urlObj.Scheme] + urlObj.Path, nil
}
return orig, nil
}
func (api *Slack) StartRTM(protocol, origin string) (*SlackWS, error) {
response := &infoResponseFull{}
err := parseResponse("rtm.start", url.Values{"token": {api.config.token}}, response, api.debug)
if err != nil {
return nil, err
}
if !response.Ok {
return nil, errors.New(response.Error)
}
api.info = response.Info
// websocket.Dial does not accept url without the port (yet)
// Fixed by: https://github.com/golang/net/commit/5058c78c3627b31e484a81463acd51c7cecc06f3
// but slack returns the address with no port, so we have to fix it
api.info.Url, err = fixUrlPort(api.info.Url)
if err != nil {
return nil, err
}
api.config.protocol, api.config.origin = protocol, origin
wsApi := &SlackWS{Slack: *api}
wsApi.conn, err = websocket.Dial(api.info.Url, api.config.protocol, api.config.origin)
if err != nil {
return nil, err
}
return wsApi, nil
}
func (api *SlackWS) Ping() error {
api.mutex.Lock()
defer api.mutex.Unlock()
api.messageId++
msg := &Ping{Id: api.messageId, Type: "ping"}
if err := websocket.JSON.Send(api.conn, msg); err != nil {
return err
}
return nil
}
func (api *SlackWS) Keepalive(interval time.Duration) {
ticker := time.NewTicker(interval)
for {
select {
case <-ticker.C:
if err := api.Ping(); err != nil {
log.Fatal(err)
}
}
}
}
func (api *SlackWS) SendMessage(msg OutgoingMessage) error {
if err := websocket.JSON.Send(api.conn, msg); err != nil {
return err
}
return nil
}
func (api *SlackWS) HandleIncomingEvents(ch *chan SlackEvent) {
event := json.RawMessage{}
for {
if err := websocket.JSON.Receive(api.conn, &event); err == io.EOF {
//log.Println("Derpi derp, should we destroy conn and start over?")
//if err = api.StartRTM(); err != nil {
// log.Fatal(err)
//}
// should we reconnect here?
if !api.conn.IsClientConn() {
api.conn, err = websocket.Dial(api.info.Url, api.config.protocol, api.config.origin)
if err != nil {
log.Panic(err)
}
}
// XXX: check for timeout and implement exponential backoff
} else if err != nil {
log.Panic(err)
}
if len(event) == 0 {
log.Println("Event Empty. WTF?")
} else {
if api.debug {
log.Println(string(event[:]))
}
handleEvent(ch, event)
}
time.Sleep(time.Millisecond * 500)
}
}
func handleEvent(ch *chan SlackEvent, event json.RawMessage) {
em := Event{}
err := json.Unmarshal(event, &em)
if err != nil {
log.Fatal(err)
}
switch em.Type {
case "":
// try ok
ack := AckMessage{}
if err = json.Unmarshal(event, &ack); err != nil {
log.Fatal(err)
}
if ack.Ok {
log.Printf("Received an ok for: %d", ack.ReplyTo)
} else {
log.Println(event)
log.Println("XXX: ?")
}
case "hello":
return
case "pong":
// XXX: Eventually check to which ping this matched with
// Allows us to have stats about latency and what not
return
case "presence_change":
//log.Printf("`%s is %s`\n", info.GetUserById(event.PUserId).Name, event.Presence)
case "message":
handleMessage(ch, event)
case "channel_marked":
log.Printf("XXX: To implement %s", em)
case "user_typing":
handleUserTyping(ch, event)
default:
log.Println("XXX: " + string(event))
}
}
func handleUserTyping(ch *chan SlackEvent, event json.RawMessage) {
msg := UserTyping{}
if err := json.Unmarshal(event, &msg); err != nil {
log.Fatal(err)
}
*ch <- SlackEvent{Type: EV_USER_TYPING, Data: msg}
}
func handleMessage(ch *chan SlackEvent, event json.RawMessage) {
msg := Message{}
err := json.Unmarshal(event, &msg)
if err != nil {
log.Fatal(err)
}
*ch <- SlackEvent{Type: EV_MESSAGE, Data: msg}
}