From 9aea1e294ad631564ecf287acee51c209eb1f293 Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Fri, 12 Apr 2024 12:57:08 +0200 Subject: [PATCH] Improve listening mode. --- stanzahandling.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/stanzahandling.go b/stanzahandling.go index 08d2a02..a95b277 100644 --- a/stanzahandling.go +++ b/stanzahandling.go @@ -20,7 +20,7 @@ import ( func sendIQ(client *xmpp.Client, iqc chan xmpp.IQ, target string, iQtype string, content string) (xmpp.IQ, error) { var iq xmpp.IQ id := getID() - c := make(chan xmpp.IQ) + c := make(chan xmpp.IQ, defaultBufferSize) go getIQ(id, c, iqc) _, err := client.RawInformation(client.JID(), target, id, iQtype, content) @@ -47,11 +47,15 @@ func getIQ(id string, c chan xmpp.IQ, iqc chan xmpp.IQ) { func rcvStanzas(client *xmpp.Client, ctx context.Context, iqc chan xmpp.IQ, msgc chan xmpp.Chat) { var received interface{} - var err error - r := make(chan interface{}) - e := make(chan error) + r := make(chan interface{}, defaultBufferSize) + e := make(chan error, defaultBufferSize) go func() { for { + select { + case <-ctx.Done(): + return + default: + } rcv, err := client.Recv() if err != nil { e <- err @@ -64,7 +68,7 @@ func rcvStanzas(client *xmpp.Client, ctx context.Context, iqc chan xmpp.IQ, msgc select { case <-ctx.Done(): return - case err = <-e: + case err := <-e: if err != nil { if err != io.EOF { closeAndExit(client, err) @@ -76,13 +80,14 @@ func rcvStanzas(client *xmpp.Client, ctx context.Context, iqc chan xmpp.IQ, msgc } switch v := received.(type) { case xmpp.Chat: + msgc <- v case xmpp.IQ: switch v.Type { case "get": var xmlns *etree.Attr iq := etree.NewDocument() - err = iq.ReadFromBytes(v.Query) + err := iq.ReadFromBytes(v.Query) if err != nil { log.Println("Couldn't parse IQ:", string(v.Query), err)