From 24ac2c05261011897bcbe6bb3a48fcff44746736 Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Fri, 12 Jan 2018 19:08:47 +0100 Subject: [PATCH] Keeps component connection open --- client.go | 2 +- cmd/xmpp_component/xmpp_component.go | 14 +++++++++++++- component.go | 6 ++++++ parser.go | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 689720e..ec62e37 100644 --- a/client.go +++ b/client.go @@ -128,7 +128,7 @@ func (c *Client) Recv() <-chan interface{} { // Send sends message text. func (c *Client) Send(packet string) error { - fmt.Fprintf(c.Session.socketProxy, packet) + fmt.Fprintf(c.Session.socketProxy, packet) // TODO handle errors return nil } diff --git a/cmd/xmpp_component/xmpp_component.go b/cmd/xmpp_component/xmpp_component.go index f08dbab..3830352 100644 --- a/cmd/xmpp_component/xmpp_component.go +++ b/cmd/xmpp_component/xmpp_component.go @@ -1,8 +1,20 @@ package main -import "fluux.io/xmpp" +import ( + "fmt" + + "fluux.io/xmpp" +) func main() { component := xmpp.Component{Host: "mqtt.localhost", Secret: "mypass"} component.Connect("localhost:8888") + + for { + _, packet, err := component.ReadPacket() + if err != nil { + return + } + fmt.Println("Packet received: ", packet) + } } diff --git a/component.go b/component.go index 55b4c48..4105700 100644 --- a/component.go +++ b/component.go @@ -88,6 +88,12 @@ func (c *Component) Connect(connStr string) error { panic("unreachable") } +// ReadPacket reads next incoming XMPP packet +// TODO use defined interface Packet +func (c *Component) ReadPacket() (xml.Name, interface{}, error) { + return next(c.decoder) +} + // ============================================================================ // XMPP packets struct diff --git a/parser.go b/parser.go index 41b778e..fac1a49 100644 --- a/parser.go +++ b/parser.go @@ -63,6 +63,7 @@ func nextStart(p *xml.Decoder) (xml.StartElement, error) { // Scan XML token stream for next element and save into val. // If val == nil, allocate new element based on proto map. // Either way, return val. +// TODO Use an interface to return packets interface xmppDecoder func next(p *xml.Decoder) (xml.Name, interface{}, error) { // Read start element to find out what type we want. se, err := nextStart(p)