example/interact.go
changeset 130 da6f37ae3ffe
parent 125 f464f14e39a7
child 137 c94a7ce0f4fb
equal deleted inserted replaced
129:cccf2b2fe34d 130:da6f37ae3ffe
     3 // license that can be found in the LICENSE file.
     3 // license that can be found in the LICENSE file.
     4 
     4 
     5 package main
     5 package main
     6 
     6 
     7 import (
     7 import (
     8 	xmpp ".."
     8 	"../xmpp"
     9 	"crypto/tls"
     9 	"crypto/tls"
    10 	"encoding/xml"
    10 	"encoding/xml"
    11 	"flag"
    11 	"flag"
    12 	"fmt"
    12 	"fmt"
    13 	"log"
    13 	"log"
    29 func init() {
    29 func init() {
    30 	logger := &StdLogger{}
    30 	logger := &StdLogger{}
    31 	// xmpp.Debug = logger
    31 	// xmpp.Debug = logger
    32 	xmpp.Info = logger
    32 	xmpp.Info = logger
    33 	xmpp.Warn = logger
    33 	xmpp.Warn = logger
    34 
       
    35 	xmpp.TlsConfig = tls.Config{InsecureSkipVerify: true}
       
    36 }
    34 }
    37 
    35 
    38 // Demonstrate the API, and allow the user to interact with an XMPP
    36 // Demonstrate the API, and allow the user to interact with an XMPP
    39 // server via the terminal.
    37 // server via the terminal.
    40 func main() {
    38 func main() {
    45 	if jid.Domain == "" || *pw == "" {
    43 	if jid.Domain == "" || *pw == "" {
    46 		flag.Usage()
    44 		flag.Usage()
    47 		os.Exit(2)
    45 		os.Exit(2)
    48 	}
    46 	}
    49 
    47 
    50 	c, err := xmpp.NewClient(&jid, *pw, nil)
    48 	tlsConf := tls.Config{InsecureSkipVerify: true}
       
    49 	c, err := xmpp.NewClient(&jid, *pw, tlsConf, nil)
    51 	if err != nil {
    50 	if err != nil {
    52 		log.Fatalf("NewClient(%v): %v", jid, err)
    51 		log.Fatalf("NewClient(%v): %v", jid, err)
    53 	}
    52 	}
    54 	defer close(c.Out)
    53 	defer close(c.Send)
    55 
    54 
    56 	err = c.StartSession(&xmpp.Presence{})
    55 	err = c.StartSession(&xmpp.Presence{})
    57 	if err != nil {
    56 	if err != nil {
    58 		log.Fatalf("StartSession: %v", err)
    57 		log.Fatalf("StartSession: %v", err)
    59 	}
    58 	}
    67 	go func(ch <-chan xmpp.Stanza) {
    66 	go func(ch <-chan xmpp.Stanza) {
    68 		for obj := range ch {
    67 		for obj := range ch {
    69 			fmt.Printf("s: %v\n", obj)
    68 			fmt.Printf("s: %v\n", obj)
    70 		}
    69 		}
    71 		fmt.Println("done reading")
    70 		fmt.Println("done reading")
    72 	}(c.In)
    71 	}(c.Recv)
    73 
    72 
    74 	p := make([]byte, 1024)
    73 	p := make([]byte, 1024)
    75 	for {
    74 	for {
    76 		nr, _ := os.Stdin.Read(p)
    75 		nr, _ := os.Stdin.Read(p)
    77 		if nr == 0 {
    76 		if nr == 0 {
   102 			fmt.Println("Can't parse non-stanza.")
   101 			fmt.Println("Can't parse non-stanza.")
   103 			continue
   102 			continue
   104 		}
   103 		}
   105 		err = dec.Decode(stan)
   104 		err = dec.Decode(stan)
   106 		if err == nil {
   105 		if err == nil {
   107 			c.Out <- stan
   106 			c.Send <- stan
   108 		} else {
   107 		} else {
   109 			fmt.Printf("Parse error: %v\n", err)
   108 			fmt.Printf("Parse error: %v\n", err)
   110 			break
   109 			break
   111 		}
   110 		}
   112 	}
   111 	}