示例#1
0
func main() {

	flag.Parse()

	// 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)
	comp := must(xmpp.NewComponentXMPP(stream, jid, *secret)).(*xmpp.XMPP)

	for x := range comp.In {
		log.Printf("recv: %v", x)
	}
}
示例#2
0
func consumer(args []string) {

	flags := flag.NewFlagSet("consumer", flag.ExitOnError)
	jidFlag := flags.String("jid", "", "JID")
	passFlag := flags.String("pass", "", "Password")
	insecureFlag := flags.Bool("insecure", false, "Allow insecure TLS")
	serverFlag := flags.String("server", "", "XMPP server address")
	flags.Parse(args)

	jid := must(xmpp.ParseJID(*jidFlag)).(xmpp.JID)
	var x *xmpp.XMPP
	if jid.Node == "" {
		stream := must(xmpp.NewStream(*serverFlag, nil)).(*xmpp.Stream)
		x = must(xmpp.NewComponentXMPP(stream, jid, *passFlag)).(*xmpp.XMPP)
	} else {
		addrs := must(xmpp.HomeServerAddrs(jid)).([]string)
		stream := must(xmpp.NewStream(addrs[0], nil)).(*xmpp.Stream)
		config := xmpp.ClientConfig{InsecureSkipVerify: *insecureFlag}
		x = must(xmpp.NewClientXMPP(stream, jid, *passFlag, &config)).(*xmpp.XMPP)
		x.Out <- xmpp.Presence{}
	}

	count := 0
	throughputCount := 0

	go func() {
		throughput := time.Tick(time.Second)
		total := time.Tick(time.Second * 5)
		for {
			select {
			case <-throughput:
				log.Printf("throughput: %d msgs/s\n", count-throughputCount)
				throughputCount = count
			case <-total:
				log.Printf("total: %d\n", count)
			}
		}
	}()

	for stanza := range x.In {
		switch v := stanza.(type) {
		case *xmpp.Message:
			count++
		case error:
			log.Fatal(v)
		}
	}
}