This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues or pull requests.
clessy/tg/client.go
2016-02-09 10:36:05 +00:00

37 lines
693 B
Go

package tg
import (
"bufio"
"encoding/json"
"io"
"log"
)
type UpdateHandler func(broker *Broker, message APIMessage)
func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) error {
broker, err := ConnectToBroker(brokerAddr)
if err != nil {
return err
}
defer broker.Close()
in := bufio.NewReader(broker.Socket)
for {
bytes, _, err := in.ReadLine()
if err != nil {
break
}
var update APIUpdate
err = json.Unmarshal(bytes, &update)
if err != nil {
log.Printf("[tg - CreateBrokerClient] ERROR reading JSON: %s\r\n", err.Error())
log.Println(string(bytes))
}
// Dispatch to UpdateHandler
updateFn(broker, update.Message)
}
return io.EOF
}