tg: Return broker instance when creating a client

This commit is contained in:
Hamcha 2017-04-21 17:20:49 +02:00
parent eb16747435
commit 15091757e3
4 changed files with 10 additions and 8 deletions

View file

@ -86,6 +86,7 @@ func isCommand(update tg.APIMessage, cmdname string) bool {
return strings.HasPrefix(text, "/"+cmdname+"@"+*botname) || (strings.HasPrefix(text, "/"+cmdname) && !strings.Contains(text, "@")) return strings.HasPrefix(text, "/"+cmdname+"@"+*botname) || (strings.HasPrefix(text, "/"+cmdname) && !strings.Contains(text, "@"))
} }
var broker *tg.Broker
var botname *string var botname *string
var impact *string var impact *string
var gillmt *string var gillmt *string
@ -123,12 +124,13 @@ func main() {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
initmods() var err error
broker, err = tg.CreateBrokerClient(*brokerAddr, dispatch)
err := tg.CreateBrokerClient(*brokerAddr, dispatch)
if err != nil { if err != nil {
panic(err) panic(err)
} }
initmods()
} }
func assert(err error) { func assert(err error) {

View file

@ -140,6 +140,6 @@ func main() {
go startPolling(*delay) go startPolling(*delay)
err := tg.CreateBrokerClient(*brokerAddr, process) _, err := tg.CreateBrokerClient(*brokerAddr, process)
assert(err) assert(err)
} }

View file

@ -42,6 +42,6 @@ func main() {
go startWebServer(*webBind) go startWebServer(*webBind)
err = tg.CreateBrokerClient(*brokerAddr, process) _, err = tg.CreateBrokerClient(*brokerAddr, process)
assert(err) assert(err)
} }

View file

@ -15,10 +15,10 @@ type BrokerCallback func(broker *Broker, update BrokerUpdate)
// CreateBrokerClient creates a connection to a broker and sends all webhook updates to a given function. // CreateBrokerClient creates a connection to a broker and sends all webhook updates to a given function.
// This is the intended way to create clients, please refer to examples for how to make a simple client. // This is the intended way to create clients, please refer to examples for how to make a simple client.
func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) error { func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) (*Broker, error) {
broker, err := ConnectToBroker(brokerAddr) broker, err := ConnectToBroker(brokerAddr)
if err != nil { if err != nil {
return err return nil, err
} }
defer broker.Close() defer broker.Close()
@ -54,5 +54,5 @@ func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) error {
// Empty buffer // Empty buffer
buf = []byte{} buf = []byte{}
} }
return io.EOF return broker, io.EOF
} }