forked from jshiffer/go-xmpp
Update example client to use router
This commit is contained in:
parent
45c7ca74b1
commit
348f29e055
30
README.md
30
README.md
@ -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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterator to receive packets coming from our XMPP connection
|
func HandleMessage(s xmpp.Sender, p xmpp.Packet) {
|
||||||
for packet := range client.Recv() {
|
msg, ok := p.(xmpp.Message)
|
||||||
switch packet := packet.(type) {
|
if !ok {
|
||||||
case xmpp.Message:
|
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)
|
||||||
_, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", packet.Body, packet.From)
|
return
|
||||||
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)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterator to receive packets coming from our XMPP connection
|
func HandleMessage(s xmpp.Sender, p xmpp.Packet) {
|
||||||
for packet := range client.Recv() {
|
msg, ok := p.(xmpp.Message)
|
||||||
switch packet := packet.(type) {
|
if !ok {
|
||||||
case xmpp.Message:
|
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)
|
||||||
_, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", packet.Body, packet.From)
|
return
|
||||||
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,
|
// TODO create default command line client to send message or to send an arbitrary XMPP sequence from a file,
|
||||||
|
Loading…
Reference in New Issue
Block a user