From 348f29e05544680232bd420c11cbe183c6aa55df Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Tue, 18 Jun 2019 12:37:16 +0200 Subject: [PATCH] Update example client to use router --- README.md | 32 ++++++++++++++++---------------- _examples/xmpp_echo/xmpp_echo.go | 30 +++++++++++++++--------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 06f9bfb..ba5d951 100644 --- a/README.md +++ b/README.md @@ -37,30 +37,30 @@ func main() { Insecure: true, } - client, err := xmpp.NewClient(config) + router := xmpp.NewRouter() + router.HandleFunc("message", HandleMessage) + + client, err := xmpp.NewClient(config, router) if err != nil { log.Fatalf("%+v", err) } // If you pass the client to a connection manager, it will handle the reconnect policy // for you automatically. - cm := xmpp.NewClientManager(client, nil) - err = cm.Start() - if err != nil { - log.Fatal(err) + cm := xmpp.NewStreamManager(client, nil) + log.Fatal(cm.Run()) +} + +func HandleMessage(s xmpp.Sender, p xmpp.Packet) { + msg, ok := p.(xmpp.Message) + if !ok { + _, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p) + return } - // Iterator to receive packets coming from our XMPP connection - for packet := range client.Recv() { - switch packet := packet.(type) { - case xmpp.Message: - _, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", packet.Body, packet.From) - reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From}, Body: packet.Body} - _ = client.Send(reply) - default: - _, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", packet) - } - } + _, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From) + reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: msg.From}, Body: msg.Body} + _ = s.Send(reply) } ``` diff --git a/_examples/xmpp_echo/xmpp_echo.go b/_examples/xmpp_echo/xmpp_echo.go index f87cdab..0bc6e92 100644 --- a/_examples/xmpp_echo/xmpp_echo.go +++ b/_examples/xmpp_echo/xmpp_echo.go @@ -21,7 +21,10 @@ func main() { Insecure: true, } - client, err := xmpp.NewClient(config) + router := xmpp.NewRouter() + router.HandleFunc("message", HandleMessage) + + client, err := xmpp.NewClient(config, router) if err != nil { log.Fatalf("%+v", err) } @@ -29,22 +32,19 @@ func main() { // If you pass the client to a connection manager, it will handle the reconnect policy // for you automatically. cm := xmpp.NewStreamManager(client, nil) - err = cm.Start() - if err != nil { - log.Fatal(err) + log.Fatal(cm.Run()) +} + +func HandleMessage(s xmpp.Sender, p xmpp.Packet) { + msg, ok := p.(xmpp.Message) + if !ok { + _, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p) + return } - // Iterator to receive packets coming from our XMPP connection - for packet := range client.Recv() { - switch packet := packet.(type) { - case xmpp.Message: - _, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", packet.Body, packet.From) - reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From}, Body: packet.Body} - _ = client.Send(reply) - default: - _, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", packet) - } - } + _, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From) + reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: msg.From}, Body: msg.Body} + _ = s.Send(reply) } // TODO create default command line client to send message or to send an arbitrary XMPP sequence from a file,