From 6ec791ba0fffb2a0c8e0e603f4f5f0cc2b6f5e18 Mon Sep 17 00:00:00 2001 From: xengineering Date: Thu, 11 May 2023 19:46:13 +0200 Subject: Refactor with new rxRoutine function --- xmpp.go | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/xmpp.go b/xmpp.go index 6540ae5..9e09700 100644 --- a/xmpp.go +++ b/xmpp.go @@ -37,25 +37,7 @@ func (l *Limox) xmpp(jid string, pwd string) { receiver := make(chan xml.Token) termination := make(chan bool) - go func() { - quit := false - dec := xml.NewDecoder(conn) - for { - select { - case <-termination: - quit = true - default: - t, _ := dec.Token() - if t != nil { - receiver <- t - } - } - if quit { - break - } - } - log.Println("Done!") - }() + go rxRoutine(conn, receiver, termination) dbg := xml.NewEncoder(os.Stdout) @@ -89,6 +71,25 @@ func (l *Limox) xmpp(jid string, pwd string) { l.XmppEvents <- XmppDisconnect } +func rxRoutine(conn *tls.Conn, tokens chan xml.Token, terminator chan bool) { + quit := false + dec := xml.NewDecoder(conn) + for { + select { + case <-terminator: + quit = true + default: + t, _ := dec.Token() + if t != nil { + tokens <- t + } + } + if quit { + break + } + } +} + func sendStreamStart(enc *xml.Encoder, jid string) (xml.EndElement) { start := xml.StartElement{ xml.Name{"jabber:client", "stream:stream"}, -- cgit v1.2.3-70-g09d2