Update example client to use router

disco_info_form
Mickael Remond 5 years ago committed by Mickaël Rémond
parent 45c7ca74b1
commit 348f29e055

@ -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())
}
// 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)
}
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
}
_, _ = 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)
}
```

@ -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())
}
// 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)
}
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
}
_, _ = 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,

Loading…
Cancel
Save