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 }