tg: Separate processing loop so we can actually have an external broker!
This commit is contained in:
parent
cf35e8b32b
commit
3977a5de53
1 changed files with 10 additions and 4 deletions
14
tg/client.go
14
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.
|
// 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) (*Broker, error) {
|
func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) error {
|
||||||
broker, err := ConnectToBroker(brokerAddr)
|
broker, err := ConnectToBroker(brokerAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
defer broker.Close()
|
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)
|
in := bufio.NewReader(broker.Socket)
|
||||||
buf := make([]byte, 0)
|
var buf []byte
|
||||||
for {
|
for {
|
||||||
bytes, isPrefix, err := in.ReadLine()
|
bytes, isPrefix, err := in.ReadLine()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -54,5 +59,6 @@ func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) (*Broker, err
|
||||||
// Empty buffer
|
// Empty buffer
|
||||||
buf = []byte{}
|
buf = []byte{}
|
||||||
}
|
}
|
||||||
return broker, io.EOF
|
|
||||||
|
return io.EOF
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue