From 3689448c90b064eefea49cd65dfde484625aa2be Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Fri, 7 Jun 2019 16:33:10 +0200 Subject: [PATCH] Adds an example directly in README file to get a feel of the API --- README.md | 53 +++++++++++++++++++++++++++++++++++++- cmd/xmpp_echo/xmpp_echo.go | 4 +-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 67d5caa..968f690 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,57 @@ The goal is to make simple to write simple adhoc XMPP clients: The library is designed to have minimal dependencies. For now, the library does not depend on any other library. -## Usage +## Example + +Here is a demo "echo" client: + +```go +package main + +import ( + "fmt" + "log" + "os" + + "gosrc.io/xmpp" +) + +func main() { + config := xmpp.Config{ + Address: "localhost:5222", + Jid: "test@localhost", + Password: "test", + PacketLogger: os.Stdout, + Insecure: true, + } + + client, err := xmpp.NewClient(config) + 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) + } + + // 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) + } + } +} +``` + +## Documentation Please, check GoDoc for more information: [gosrc.io/xmpp](https://godoc.org/gosrc.io/xmpp) diff --git a/cmd/xmpp_echo/xmpp_echo.go b/cmd/xmpp_echo/xmpp_echo.go index 94fc087..0677e0c 100644 --- a/cmd/xmpp_echo/xmpp_echo.go +++ b/cmd/xmpp_echo/xmpp_echo.go @@ -1,5 +1,5 @@ /* -xmpp_client is a demo client that connect on an XMPP server and echo message received back to original sender. +xmpp_echo is a demo client that connect on an XMPP server and echo message received back to original sender. */ package main @@ -30,8 +30,6 @@ func main() { // for you automatically. cm := xmpp.NewClientManager(client, nil) err = cm.Start() - // connection can be stopped with cm.Stop() - // connection state can be checked by reading cm.Client.CurrentState if err != nil { log.Fatal(err) }