Ejemplo n.º 1
0
func main() {
	turnpike.Debug()
	c, err := turnpike.NewWebsocketClient(turnpike.JSON, "ws://localhost:8000/")
	if err != nil {
		log.Fatal(err)
	}
	_, err = c.JoinRealm("turnpike.examples", nil)
	if err != nil {
		log.Fatal(err)
	}

	quit := make(chan bool)
	c.Subscribe("alarm.ring", func([]interface{}, map[string]interface{}) {
		fmt.Println("The alarm rang!")
		c.Close()
		quit <- true
	})
	fmt.Print("Enter the timer duration: ")
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	if err := scanner.Err(); err != nil {
		log.Fatalln("reading stdin:", err)
	}
	text := scanner.Text()
	if duration, err := strconv.Atoi(text); err != nil {
		log.Fatalln("invalid integer input:", err)
	} else {
		if _, err := c.Call("alarm.set", []interface{}{duration}, nil); err != nil {
			log.Fatalln("error setting alarm:", err)
		}
	}
	<-quit
}
Ejemplo n.º 2
0
func main() {
	c, err := turnpike.NewWebsocketClient(turnpike.JSON, "ws://localhost:8000/")
	if err != nil {
		log.Fatal(err)
	}
	log.Println("connected to router")
	_, err = c.JoinRealm("turnpike.examples", nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("joined realm")
	c.ReceiveDone = make(chan bool)

	onJoin := func(args []interface{}, kwargs map[string]interface{}) {
		log.Println("session joined:", args[0])
	}
	if err := c.Subscribe("wamp.session.on_join", onJoin); err != nil {
		log.Fatalln("Error subscribing to channel:", err)
	}

	onLeave := func(args []interface{}, kwargs map[string]interface{}) {
		log.Println("session left:", args[0])
	}
	if err := c.Subscribe("wamp.session.on_leave", onLeave); err != nil {
		log.Fatalln("Error subscribing to channel:", err)
	}

	log.Println("listening for meta events")
	<-c.ReceiveDone
	log.Println("disconnected")
}
Ejemplo n.º 3
0
func main() {
	if len(os.Args) != 2 {
		log.Fatalf("Usage: %s <username>", os.Args[0])
	}

	if fi, err := os.Stderr.Stat(); err != nil {
		log.Fatal("Error checking stderr:", err)
	} else if fi.Mode()&os.ModeDevice != 0 {
		stderr, err := os.Open(os.DevNull)
		if err != nil {
			log.Fatal("Error redirecting stderr")
		}
		log.SetOutput(stderr)
	}

	username := os.Args[1]

	// turnpike.Debug()
	c, err := turnpike.NewWebsocketClient(turnpike.JSON, "ws://localhost:8000/")
	if err != nil {
		log.Fatal(err)
	}
	_, err = c.JoinRealm("turnpike.examples", nil)
	if err != nil {
		log.Fatal(err)
	}

	messages := make(chan message)
	if err := c.Subscribe("chat", func(args []interface{}, kwargs map[string]interface{}) {
		if len(args) == 2 {
			if from, ok := args[0].(string); !ok {
				log.Println("First argument not a string:", args[0])
			} else if msg, ok := args[1].(string); !ok {
				log.Println("Second argument not a string:", args[1])
			} else {
				log.Printf("%s: %s", from, msg)
				messages <- message{From: from, Message: msg}
			}
		}
	}); err != nil {
		log.Fatalln("Error subscribing to chat channel:", err)
	}

	outgoing := make(chan message, 10)
	go sendMessages(c, outgoing)
	cw := newChatWin(username, outgoing)
	cw.dialog(messages)
}
Ejemplo n.º 4
0
func main() {
	turnpike.Debug()
	fmt.Println("Hint: the password is 'password'")
	fmt.Print("Password: "******"ws://localhost:8000/ws")
	if err != nil {
		log.Fatal(err)
	}
	c.Auth = map[string]turnpike.AuthFunc{"example-auth": exampleAuthFunc}
	_, err = c.JoinRealm("turnpike.examples", nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Connected with auth")
	time.Sleep(3 * time.Second)
	fmt.Println("Disconnecting")
	c.Close()
}