Make example code easier to read with a bit of log-and-die trickery.
This commit is contained in:
parent
424c06855c
commit
909d0f5fac
27
client.go
27
client.go
|
|
@ -14,29 +14,19 @@ var (
|
|||
|
||||
func main() {
|
||||
|
||||
// Parse args.
|
||||
flag.Parse()
|
||||
jid, _ := xmpp.ParseJID(*jid)
|
||||
password := *password
|
||||
|
||||
// Create stream.
|
||||
stream, err := xmpp.NewStream(jid.Domain + ":5222", &xmpp.StreamConfig{LogStanzas: true})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Configure stream as a client connection.
|
||||
x, err := xmpp.NewClientXMPP(stream, jid, password, &xmpp.ClientConfig{InsecureSkipVerify: true})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Create stream and configure it as a client connection.
|
||||
jid := must(xmpp.ParseJID(*jid)).(xmpp.JID)
|
||||
stream := must(xmpp.NewStream(jid.Domain + ":5222", &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream)
|
||||
x := must(xmpp.NewClientXMPP(stream, jid, *password, &xmpp.ClientConfig{InsecureSkipVerify: true})).(*xmpp.XMPP)
|
||||
|
||||
log.Printf("Connection established for %s\n", x.JID)
|
||||
|
||||
// Announce 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(
|
||||
func(v interface{}) bool {
|
||||
_, ok := v.(*xmpp.Message)
|
||||
|
|
@ -71,6 +61,13 @@ func main() {
|
|||
select {}
|
||||
}
|
||||
|
||||
func must(v interface{}, err error) interface{} {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
type DiscoInfo struct {
|
||||
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"`
|
||||
Identity []DiscoIdentity `xml:"identity"`
|
||||
|
|
|
|||
26
component.go
26
component.go
|
|
@ -13,22 +13,13 @@ var (
|
|||
)
|
||||
|
||||
func main() {
|
||||
|
||||
flag.Parse()
|
||||
addr := *addr
|
||||
jid, _ := xmpp.ParseJID(*jid)
|
||||
secret := *secret
|
||||
|
||||
// Create stream.
|
||||
stream, err := xmpp.NewStream(addr, &xmpp.StreamConfig{LogStanzas: true})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Configure stream as a component connection.
|
||||
x, err := xmpp.NewComponentXMPP(stream, jid, secret)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Create stream and configure it as a component connection.
|
||||
jid := must(xmpp.ParseJID(*jid)).(xmpp.JID)
|
||||
stream := must(xmpp.NewStream(*addr, &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream)
|
||||
x := must(xmpp.NewComponentXMPP(stream, jid, *secret)).(*xmpp.XMPP)
|
||||
|
||||
for {
|
||||
v, err := x.Recv()
|
||||
|
|
@ -38,3 +29,10 @@ func main() {
|
|||
log.Printf("recv: %v", v)
|
||||
}
|
||||
}
|
||||
|
||||
func must(v interface{}, err error) interface{} {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue