From f3218c4afa0df46b9f3819b06c913a1cf9eb0952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?CORNIERE=20R=C3=A9mi?= Date: Tue, 14 Jan 2020 19:12:54 +0100 Subject: [PATCH] PubSub example --- .../xmpp_pubsub_client/xmpp_ps_client.go | 182 ++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 _examples/xmpp_pubsub_client/xmpp_ps_client.go diff --git a/_examples/xmpp_pubsub_client/xmpp_ps_client.go b/_examples/xmpp_pubsub_client/xmpp_ps_client.go new file mode 100644 index 0000000..972b94d --- /dev/null +++ b/_examples/xmpp_pubsub_client/xmpp_ps_client.go @@ -0,0 +1,182 @@ +package main + +import ( + "context" + "encoding/xml" + "fmt" + "gosrc.io/xmpp" + "gosrc.io/xmpp/stanza" + "log" + "time" +) + +const ( + userJID = "testuser2@localhost" + serverAddress = "localhost:5222" + nodeName = "lel_node" + serviceName = "pubsub.localhost" +) + +func main() { + + config := xmpp.Config{ + TransportConfiguration: xmpp.TransportConfiguration{ + Address: serverAddress, + }, + Jid: userJID, + Credential: xmpp.Password("pass123"), + // StreamLogger: os.Stdout, + Insecure: true, + } + router := xmpp.NewRouter() + router.NewRoute().Packet("message"). + HandlerFunc(func(s xmpp.Sender, p stanza.Packet) { + data, _ := xml.Marshal(p) + fmt.Println("Received a publication ! => \n" + string(data)) + }) + + client, err := xmpp.NewClient(config, router, func(err error) { fmt.Println(err) }) + if err != nil { + log.Fatalf("%+v", err) + } + + // ========================== + // Client connection + err = client.Connect() + if err != nil { + log.Fatalf("%+v", err) + } + + // ========================== + // Create a node + rqCreate, err := stanza.NewCreateNode(serviceName, nodeName) + if err != nil { + log.Fatalf("%+v", err) + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + createCh, err := client.SendIQ(ctx, rqCreate) + if err != nil { + log.Fatalf("%+v", err) + } else { + + if createCh != nil { + select { + case respCr := <-createCh: + // Got response from server + if respCr.Error != nil { + if respCr.Error.Reason != "conflict" { + log.Fatalf("%+v", respCr.Error.Text) + } + fmt.Println(respCr.Error.Text) + } else { + fmt.Print("successfully created channel") + } + case <-time.After(100 * time.Millisecond): + cancel() + log.Fatal("No iq response was received in time") + } + } + } + + // ==================================== + // Now let's subscribe to this node : + rqSubscribe, _ := stanza.NewSubRq(serviceName, stanza.SubInfo{ + Node: nodeName, + Jid: userJID, + }) + if err != nil { + log.Fatalf("%+v", err) + } + subRespCh, _ := client.SendIQ(ctx, rqSubscribe) + if subRespCh != nil { + select { + case <-subRespCh: + fmt.Println("Subscribed to the service") + case <-time.After(100 * time.Millisecond): + cancel() + log.Fatal("No iq response was received in time") + } + } + + // ========================== + // Publish to that node + pub, err := stanza.NewPublishItemRq(serviceName, nodeName, "", stanza.Item{ + Publisher: "testuser2", + Any: &stanza.Node{ + XMLName: xml.Name{ + Space: "http://www.w3.org/2005/Atom", + Local: "entry", + }, + Nodes: []stanza.Node{ + { + XMLName: xml.Name{Space: "", Local: "title"}, + Attrs: nil, + Content: "My pub item title", + Nodes: nil, + }, + { + XMLName: xml.Name{Space: "", Local: "summary"}, + Attrs: nil, + Content: "My pub item content summary", + Nodes: nil, + }, + { + XMLName: xml.Name{Space: "", Local: "link"}, + Attrs: []xml.Attr{ + { + Name: xml.Name{Space: "", Local: "rel"}, + Value: "alternate", + }, + { + Name: xml.Name{Space: "", Local: "type"}, + Value: "text/html", + }, + { + Name: xml.Name{Space: "", Local: "href"}, + Value: "http://denmark.lit/2003/12/13/atom03", + }, + }, + }, + { + XMLName: xml.Name{Space: "", Local: "id"}, + Attrs: nil, + Content: "My pub item content ID", + Nodes: nil, + }, + { + XMLName: xml.Name{Space: "", Local: "published"}, + Attrs: nil, + Content: "2003-12-13T18:30:02Z", + Nodes: nil, + }, + { + XMLName: xml.Name{Space: "", Local: "updated"}, + Attrs: nil, + Content: "2003-12-13T18:30:02Z", + Nodes: nil, + }, + }, + }, + }) + + if err != nil { + log.Fatalf("%+v", err) + } + pubRespCh, _ := client.SendIQ(ctx, pub) + if pubRespCh != nil { + select { + case <-pubRespCh: + fmt.Println("Published item to the service") + case <-time.After(100 * time.Millisecond): + cancel() + log.Fatal("No iq response was received in time") + } + } + + // ============================= + // Let's purge the node now : + purgeRq, _ := stanza.NewPurgeAllItems(serviceName, nodeName) + client.SendIQ(ctx, purgeRq) + + cancel() +}