example/interact.go
changeset 183 b4bd77d58a3e
parent 178 ccfebbd9f49b
equal deleted inserted replaced
182:626c390682fc 183:b4bd77d58a3e
     9 	"log"
     9 	"log"
    10 	"os"
    10 	"os"
    11 	"strings"
    11 	"strings"
    12 )
    12 )
    13 
    13 
    14 type StdLogger struct {
       
    15 }
       
    16 
       
    17 func (s *StdLogger) Log(v ...interface{}) {
       
    18 	log.Println(v...)
       
    19 }
       
    20 
       
    21 func (s *StdLogger) Logf(fmt string, v ...interface{}) {
       
    22 	log.Printf(fmt, v...)
       
    23 }
       
    24 
       
    25 func init() {
    14 func init() {
    26 	logger := &StdLogger{}
    15 	// xmpp.Debug = true
    27 	// xmpp.Debug = logger
       
    28 	xmpp.Info = logger
       
    29 	xmpp.Warn = logger
       
    30 }
    16 }
    31 
    17 
    32 // Demonstrate the API, and allow the user to interact with an XMPP
    18 // Demonstrate the API, and allow the user to interact with an XMPP
    33 // server via the terminal.
    19 // server via the terminal.
    34 func main() {
    20 func main() {
    35 	var jid xmpp.JID
    21 	jidStr := flag.String("jid", "", "JID to log in as")
    36 	flag.Var(&jid, "jid", "JID to log in as")
    22 	pw := flag.String("pw", "", "password")
    37 	var pw *string = flag.String("pw", "", "password")
       
    38 	flag.Parse()
    23 	flag.Parse()
    39 	if jid.Domain == "" || *pw == "" {
    24 	jid := xmpp.JID(*jidStr)
       
    25 	if jid.Domain() == "" || *pw == "" {
    40 		flag.Usage()
    26 		flag.Usage()
    41 		os.Exit(2)
    27 		os.Exit(2)
    42 	}
    28 	}
    43 
    29 
       
    30 	stat := make(chan xmpp.Status)
       
    31 	go func() {
       
    32 		for s := range stat {
       
    33 			log.Printf("connection status %d", s)
       
    34 		}
       
    35 	}()
    44 	tlsConf := tls.Config{InsecureSkipVerify: true}
    36 	tlsConf := tls.Config{InsecureSkipVerify: true}
    45 	c, err := xmpp.NewClient(&jid, *pw, tlsConf, nil, xmpp.Presence{}, nil)
    37 	c, err := xmpp.NewClient(&jid, *pw, tlsConf, nil, xmpp.Presence{}, stat)
    46 	if err != nil {
    38 	if err != nil {
    47 		log.Fatalf("NewClient(%v): %v", jid, err)
    39 		log.Fatalf("NewClient(%v): %v", jid, err)
    48 	}
    40 	}
    49 	defer close(c.Send)
    41 	defer c.Close()
    50 
    42 
    51 	go func(ch <-chan xmpp.Stanza) {
    43 	go func(ch <-chan xmpp.Stanza) {
    52 		for obj := range ch {
    44 		for obj := range ch {
    53 			fmt.Printf("s: %v\n", obj)
    45 			fmt.Printf("s: %v\n", obj)
    54 		}
    46 		}