1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package main
- import (
- "encoding/json"
- "log"
- "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 {
- 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)
- }
- }
|