|
@@ -1,28 +1,51 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
- "fmt"
|
|
|
+ "encoding/json"
|
|
|
"log"
|
|
|
- "math/rand"
|
|
|
+ "net/http"
|
|
|
"time"
|
|
|
|
|
|
"github.com/ably/ably-go/ably"
|
|
|
)
|
|
|
|
|
|
+type Resp struct {
|
|
|
+ Type string `json:"type"`
|
|
|
+ Value struct {
|
|
|
+ ID int `json:"id"`
|
|
|
+ Joke string `json:"joke"`
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func main() {
|
|
|
opt := &ably.ClientOptions{}
|
|
|
opt.Key = "kY92tg.1qo8DQ:CS755GfXwEGtBiIr"
|
|
|
a, err := ably.NewRealtimeClient(opt)
|
|
|
panicIfErr(err)
|
|
|
chn := a.Channels.Get("test")
|
|
|
+
|
|
|
for {
|
|
|
- log.Println("Sending:")
|
|
|
- chn.Publish("test", fmt.Sprintf("Hello: %d", rand.Intn(10)))
|
|
|
+ processMsg(chn)
|
|
|
<-time.After(2 * time.Second)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
+func processMsg(chn *ably.RealtimeChannel) {
|
|
|
+ var data Resp
|
|
|
+ resp, err := http.Get("http://api.icndb.com/jokes/random")
|
|
|
+ if err != nil {
|
|
|
+ log.Println("Error fetching http", err)
|
|
|
+ return // no message
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+ json.NewDecoder(resp.Body).Decode(&data)
|
|
|
+
|
|
|
+ log.Println("Sending..")
|
|
|
+ chn.Publish("test", data.Value.Joke)
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
func panicIfErr(err error) {
|
|
|
if err != nil {
|
|
|
panic(err)
|