1
0
Fork 0

Make example code easier to read with a bit of log-and-die trickery.

This commit is contained in:
Matt Goodall 2012-07-12 00:02:49 +01:00
parent 424c06855c
commit 909d0f5fac
2 changed files with 24 additions and 29 deletions

View File

@ -14,29 +14,19 @@ var (
func main() { func main() {
// Parse args.
flag.Parse() flag.Parse()
jid, _ := xmpp.ParseJID(*jid)
password := *password
// Create stream. // Create stream and configure it as a client connection.
stream, err := xmpp.NewStream(jid.Domain + ":5222", &xmpp.StreamConfig{LogStanzas: true}) jid := must(xmpp.ParseJID(*jid)).(xmpp.JID)
if err != nil { stream := must(xmpp.NewStream(jid.Domain + ":5222", &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream)
log.Fatal(err) x := must(xmpp.NewClientXMPP(stream, jid, *password, &xmpp.ClientConfig{InsecureSkipVerify: true})).(*xmpp.XMPP)
}
// Configure stream as a client connection.
x, err := xmpp.NewClientXMPP(stream, jid, password, &xmpp.ClientConfig{InsecureSkipVerify: true})
if err != nil {
log.Fatal(err)
}
log.Printf("Connection established for %s\n", x.JID) log.Printf("Connection established for %s\n", x.JID)
// Announce presence. // Announce presence.
x.Send(xmpp.Presence{}) x.Send(xmpp.Presence{})
// Filter messages into dedicated channel and start a thread to log them. // Filter messages into dedicated channel and start a goroutine to log them.
_, messages := x.AddFilter( _, messages := x.AddFilter(
func(v interface{}) bool { func(v interface{}) bool {
_, ok := v.(*xmpp.Message) _, ok := v.(*xmpp.Message)
@ -71,6 +61,13 @@ func main() {
select {} select {}
} }
func must(v interface{}, err error) interface{} {
if err != nil {
log.Fatal(err)
}
return v
}
type DiscoInfo struct { type DiscoInfo struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"` XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"`
Identity []DiscoIdentity `xml:"identity"` Identity []DiscoIdentity `xml:"identity"`

View File

@ -13,22 +13,13 @@ var (
) )
func main() { func main() {
flag.Parse() flag.Parse()
addr := *addr
jid, _ := xmpp.ParseJID(*jid)
secret := *secret
// Create stream. // Create stream and configure it as a component connection.
stream, err := xmpp.NewStream(addr, &xmpp.StreamConfig{LogStanzas: true}) jid := must(xmpp.ParseJID(*jid)).(xmpp.JID)
if err != nil { stream := must(xmpp.NewStream(*addr, &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream)
log.Fatal(err) x := must(xmpp.NewComponentXMPP(stream, jid, *secret)).(*xmpp.XMPP)
}
// Configure stream as a component connection.
x, err := xmpp.NewComponentXMPP(stream, jid, secret)
if err != nil {
log.Fatal(err)
}
for { for {
v, err := x.Recv() v, err := x.Recv()
@ -38,3 +29,10 @@ func main() {
log.Printf("recv: %v", v) log.Printf("recv: %v", v)
} }
} }
func must(v interface{}, err error) interface{} {
if err != nil {
log.Fatal(err)
}
return v
}