forked from jshiffer/go-xmpp
Basic test component (disco Info)
This commit is contained in:
parent
ceeb51ce0e
commit
ff2da776d3
@ -7,11 +7,12 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
component := xmpp.Component{Host: "mqtt.localhost", Secret: "mypass"}
|
||||
component.Connect("localhost:8888")
|
||||
component := MyComponent{Name: "MQTT Component", Category: "gateway", Type: "mqtt"}
|
||||
component.xmpp = &xmpp.Component{Host: "mqtt.localhost", Secret: "mypass"}
|
||||
component.xmpp.Connect("localhost:8888")
|
||||
|
||||
for {
|
||||
packet, err := component.ReadPacket()
|
||||
packet, err := component.xmpp.ReadPacket()
|
||||
if err != nil {
|
||||
fmt.Println("read error", err)
|
||||
return
|
||||
@ -21,7 +22,7 @@ func main() {
|
||||
case xmpp.IQ:
|
||||
switch inner := p.Payload.(type) {
|
||||
case *xmpp.Node:
|
||||
fmt.Println("Node:", inner.XMLName.Space, inner.XMLName.Local)
|
||||
component.processIQ(p.Type, p.Id, p.From, inner)
|
||||
default:
|
||||
fmt.Println("default")
|
||||
}
|
||||
@ -30,3 +31,48 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
NSDiscoInfo = "http://jabber.org/protocol/disco#info"
|
||||
)
|
||||
|
||||
type MyComponent struct {
|
||||
Name string
|
||||
// Typical categories and types: https://xmpp.org/registrar/disco-categories.html
|
||||
Category string
|
||||
Type string
|
||||
|
||||
xmpp *xmpp.Component
|
||||
}
|
||||
|
||||
func (c MyComponent) processIQ(iqType, id, from string, inner *xmpp.Node) {
|
||||
fmt.Println("Node:", inner.XMLName.Space, inner.XMLName.Local)
|
||||
switch inner.XMLName.Space + " " + iqType {
|
||||
case NSDiscoInfo + " get":
|
||||
fmt.Println("Send Disco Info")
|
||||
result := fmt.Sprintf(`<iq type='result'
|
||||
from='%s'
|
||||
to='%s'
|
||||
id='%s'>
|
||||
<query xmlns='http://jabber.org/protocol/disco#info'>
|
||||
<identity
|
||||
category='%s'
|
||||
type='%s'
|
||||
name='%s'/>
|
||||
<feature var='http://jabber.org/protocol/disco#info'/>
|
||||
<feature var='http://jabber.org/protocol/disco#items'/>
|
||||
</query>
|
||||
</iq>`, c.xmpp.Host, from, id, c.Category, c.Type, c.Name)
|
||||
c.xmpp.Send(result)
|
||||
default:
|
||||
iqErr := fmt.Sprintf(`<iq type='error'
|
||||
from='%s'
|
||||
to='%s'
|
||||
id='%s'>
|
||||
<error type="cancel" code="501">
|
||||
<feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
|
||||
</error>
|
||||
</iq>`, c.xmpp.Host, from, id)
|
||||
c.xmpp.Send(iqErr)
|
||||
}
|
||||
}
|
||||
|
@ -94,6 +94,13 @@ func (c *Component) ReadPacket() (Packet, error) {
|
||||
return next(c.decoder)
|
||||
}
|
||||
|
||||
func (c *Component) Send(packet string) error {
|
||||
if _, err := fmt.Fprintf(c.conn, packet); err != nil {
|
||||
return errors.New("cannot send packet " + err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Handshake Packet
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user