simplify websocket example
I'm not sure if I missed something but I think the example could be simplified to this.
I changed it to directly loop over the channel
and I replaced the "break with label" with a "return".
jorin authored 7 years ago
Jorin Vogel committed 7 years ago
16 | 16 | rtm := api.NewRTM() |
17 | 17 | go rtm.ManageConnection() |
18 | 18 | |
19 | Loop: | |
20 | for { | |
21 | select { | |
22 | case msg := <-rtm.IncomingEvents: | |
23 | fmt.Print("Event Received: ") | |
24 | switch ev := msg.Data.(type) { | |
25 | case *slack.HelloEvent: | |
26 | // Ignore hello | |
19 | for msg := range rtm.IncomingEvents { | |
20 | fmt.Print("Event Received: ") | |
21 | switch ev := msg.Data.(type) { | |
22 | case *slack.HelloEvent: | |
23 | // Ignore hello | |
27 | 24 | |
28 | case *slack.ConnectedEvent: | |
29 | fmt.Println("Infos:", ev.Info) | |
30 | fmt.Println("Connection counter:", ev.ConnectionCount) | |
31 | // Replace #general with your Channel ID | |
32 | rtm.SendMessage(rtm.NewOutgoingMessage("Hello world", "#general")) | |
25 | case *slack.ConnectedEvent: | |
26 | fmt.Println("Infos:", ev.Info) | |
27 | fmt.Println("Connection counter:", ev.ConnectionCount) | |
28 | // Replace #general with your Channel ID | |
29 | rtm.SendMessage(rtm.NewOutgoingMessage("Hello world", "#general")) | |
33 | 30 | |
34 | case *slack.MessageEvent: | |
35 | fmt.Printf("Message: %v\n", ev) | |
31 | case *slack.MessageEvent: | |
32 | fmt.Printf("Message: %v\n", ev) | |
36 | 33 | |
37 | case *slack.PresenceChangeEvent: | |
38 | fmt.Printf("Presence Change: %v\n", ev) | |
34 | case *slack.PresenceChangeEvent: | |
35 | fmt.Printf("Presence Change: %v\n", ev) | |
39 | 36 | |
40 | case *slack.LatencyReport: | |
41 | fmt.Printf("Current latency: %v\n", ev.Value) | |
37 | case *slack.LatencyReport: | |
38 | fmt.Printf("Current latency: %v\n", ev.Value) | |
42 | 39 | |
43 | case *slack.RTMError: | |
44 | fmt.Printf("Error: %s\n", ev.Error()) | |
40 | case *slack.RTMError: | |
41 | fmt.Printf("Error: %s\n", ev.Error()) | |
45 | 42 | |
46 | case *slack.InvalidAuthEvent: | |
47 | fmt.Printf("Invalid credentials") | |
48 | break Loop | |
43 | case *slack.InvalidAuthEvent: | |
44 | fmt.Printf("Invalid credentials") | |
45 | return | |
49 | 46 | |
50 | default: | |
47 | default: | |
51 | 48 | |
52 | // Ignore other events.. | |
53 | // fmt.Printf("Unexpected: %v\n", msg.Data) | |
54 | } | |
49 | // Ignore other events.. | |
50 | // fmt.Printf("Unexpected: %v\n", msg.Data) | |
55 | 51 | } |
56 | 52 | } |
57 | 53 | } |