From 3977a5de532a2a938fc248c9cd4bf4b443bbe60a Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 4 May 2017 16:54:23 +0200 Subject: [PATCH] tg: Separate processing loop so we can actually have an external broker! --- tg/client.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tg/client.go b/tg/client.go index 2cd2bbe..af3002b 100644 --- a/tg/client.go +++ b/tg/client.go @@ -15,15 +15,20 @@ type BrokerCallback func(broker *Broker, update BrokerUpdate) // 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. -func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) (*Broker, error) { +func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) error { broker, err := ConnectToBroker(brokerAddr) if err != nil { - return nil, err + return err } defer broker.Close() + return RunBrokerClient(broker, updateFn) +} + +// RunBrokerClient is a slimmer version of CreateBrokerClient for who wants to keep its own broker connection +func RunBrokerClient(broker *Broker, updateFn UpdateHandler) error { in := bufio.NewReader(broker.Socket) - buf := make([]byte, 0) + var buf []byte for { bytes, isPrefix, err := in.ReadLine() if err != nil { @@ -54,5 +59,6 @@ func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) (*Broker, err // Empty buffer buf = []byte{} } - return broker, io.EOF + + return io.EOF }