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() {
	turnpike.Debug()
	s := turnpike.NewBasicWebsocketServer("turnpike.chat.realm")
	http.Handle("/", http.FileServer(http.Dir(".")))
	http.Handle("/ws", s)
	log.Println("turnpike server starting on port 8000")
	log.Println("Hint: start clicking on the web page(s) you open to localhost:8000")
	log.Fatal(http.ListenAndServe(":8000", nil))
}
Ejemplo n.º 3
0
func main() {
	turnpike.Debug()
	s := turnpike.NewBasicWebsocketServer("turnpike.examples")
	server := &http.Server{
		Handler: s,
		Addr:    ":8000",
	}
	log.Println("turnpike server starting on port 8000")
	log.Fatal(server.ListenAndServe())
}
Ejemplo n.º 4
0
func main() {
	turnpike.Debug()
	s := turnpike.NewBasicWebsocketServer("turnpike.examples")
	server := &http.Server{
		Handler: s,
		Addr:    ":8000",
	}
	client, _ = s.GetLocalClient("turnpike.examples", nil)
	if err := client.BasicRegister("alarm.set", alarmSet); err != nil {
		panic(err)
	}
	log.Println("turnpike server starting on port 8000")
	log.Fatal(server.ListenAndServe())
}
Ejemplo n.º 5
0
func main() {
	turnpike.Debug()
	s, err := turnpike.NewWebsocketServer(map[string]turnpike.Realm{
		"turnpike.examples": {
			CRAuthenticators: map[string]turnpike.CRAuthenticator{
				"example-auth": &exampleAuth{password: "******"},
			},
		},
	})
	if err != nil {
		panic("error creating websocket server: " + err.Error())
	}
	http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
		log.Println("Connection from", r.RemoteAddr)
		s.ServeHTTP(w, r)
	})
	log.Println("turnpike server starting on port 8000")
	log.Fatal(http.ListenAndServe(":8000", nil))
}
Ejemplo n.º 6
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()
}
Ejemplo n.º 7
0
func main() {
	flag.Parse()
	if debug {
		turnpike.Debug()
	}
	s := turnpike.NewBasicWebsocketServer(realm)
	shutdown := make(chan os.Signal, 1)
	signal.Notify(shutdown, os.Interrupt)
	go func() {
		<-shutdown
		s.Close()
		log.Println("shutting down server...")
		time.Sleep(time.Second)
		os.Exit(1)
	}()

	server := &http.Server{
		Handler: s,
		Addr:    ":8000",
	}
	log.Printf("turnpike server starting on port %d...", port)
	log.Fatal(server.ListenAndServe())
}