Ejemplo n.º 1
0
func main() {
	fmt.Println("sito")
	go h.run()
	go interval()

	port := os.Getenv("PORT")
	if port == "" {
		log.WithField("PORT", port).Fatal("$PORT must be set")
	}

	mux := http.NewServeMux()
	mux.HandleFunc("/ws", handleWebsocket)
	mux.Handle("/", http.FileServer(http.Dir(rootPath+"public")))

	n := negroni.Classic()
	n.UseHandler(mux)
	n.Run(":" + port)

}
Ejemplo n.º 2
0
func handleWebsocket(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "Method not allowed", 405)
		return
	}
	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.WithField("err", err).Println("Upgrading to websockets")
		http.Error(w, "Error Upgrading to websockets", 400)
		return
	}
	log.Info("NEW CONNECTION")

	c := &client{
		send: make(chan []byte, maxMessageSize),
		ws:   ws,
		id:   h.id,
	}
	h.id += 1
	h.register <- c
	go handleMessage(c)

}