Package list golang-github-nlopes-slack / d8daca0
Simplify example. Fixed an EOF error, which seemingly is sent even though the socket is not closed.. does it mean there are no other events to read ? Alexandre Bourget authored 6 years ago Norberto Lopes committed 6 years ago
3 changed file(s) with 8 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
66 )
77
88 func main() {
9 chSender := make(chan slack.OutgoingMessage)
10
119 api := slack.New("YOUR TOKEN HERE")
1210 api.SetDebug(true)
1311
1412 rtm := api.NewRTM()
1513 go rtm.ManageConnection()
16
17 go func(rtm *slack.RTM, chSender chan slack.OutgoingMessage) {
18 for {
19 select {
20 case msg := <-chSender:
21 rtm.SendMessage(&msg)
22 }
23 }
24 }(rtm, chSender)
2514
2615 for {
2716 select {
99
1010 // RTM represents a managed websocket connection. It also supports
1111 // all the methods of the `Slack` type.
12 //
13 // Create this element with Client's NewRTM().
1214 type RTM struct {
1315 mutex sync.Mutex
1416 messageID int
22 import (
33 "encoding/json"
44 "fmt"
5 "io"
56 "reflect"
67 "time"
78
114115 rtm.pings[rtm.messageID] = time.Now()
115116
116117 msg := &Ping{Id: rtm.messageID, Type: "ping"}
118 rtm.Debugln("Sending PING")
117119 if err := websocket.JSON.Send(conn, msg); err != nil {
118120 errors <- fmt.Errorf("error sending 'ping': %s", err)
119121 }
153155
154156 event := json.RawMessage{}
155157 err := websocket.JSON.Receive(conn, &event)
156 if err != nil {
158 if err == io.EOF {
159 rtm.Debugln("GOT EOF, are we killing ??")
160 } else if err != nil {
157161 errors <- err
158162 return
159163 }
160164 if len(event) == 0 {
161 //log.Println("Event Empty. WTF?")
165 rtm.Debugln("Event Empty. WTF?")
162166 continue
163167 }
164168