Update example client to use router

This commit is contained in:
Mickael Remond 2019-06-18 12:37:16 +02:00 committed by Mickaël Rémond
parent 45c7ca74b1
commit 348f29e055
2 changed files with 31 additions and 31 deletions

View file

@ -37,30 +37,30 @@ func main() {
Insecure: true, Insecure: true,
} }
client, err := xmpp.NewClient(config) router := xmpp.NewRouter()
router.HandleFunc("message", HandleMessage)
client, err := xmpp.NewClient(config, router)
if err != nil { if err != nil {
log.Fatalf("%+v", err) log.Fatalf("%+v", err)
} }
// If you pass the client to a connection manager, it will handle the reconnect policy // If you pass the client to a connection manager, it will handle the reconnect policy
// for you automatically. // for you automatically.
cm := xmpp.NewClientManager(client, nil) cm := xmpp.NewStreamManager(client, nil)
err = cm.Start() log.Fatal(cm.Run())
if err != nil { }
log.Fatal(err)
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 _, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From)
for packet := range client.Recv() { reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: msg.From}, Body: msg.Body}
switch packet := packet.(type) { _ = s.Send(reply)
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)
}
}
} }
``` ```

View file

@ -21,7 +21,10 @@ func main() {
Insecure: true, Insecure: true,
} }
client, err := xmpp.NewClient(config) router := xmpp.NewRouter()
router.HandleFunc("message", HandleMessage)
client, err := xmpp.NewClient(config, router)
if err != nil { if err != nil {
log.Fatalf("%+v", err) 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 // If you pass the client to a connection manager, it will handle the reconnect policy
// for you automatically. // for you automatically.
cm := xmpp.NewStreamManager(client, nil) cm := xmpp.NewStreamManager(client, nil)
err = cm.Start() log.Fatal(cm.Run())
if err != nil { }
log.Fatal(err)
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 _, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From)
for packet := range client.Recv() { reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: msg.From}, Body: msg.Body}
switch packet := packet.(type) { _ = s.Send(reply)
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)
}
}
} }
// TODO create default command line client to send message or to send an arbitrary XMPP sequence from a file, // TODO create default command line client to send message or to send an arbitrary XMPP sequence from a file,