Ejemplo n.º 1
0
func main() {
	router := &Router{
		connections: make(map[*rhynock.Conn]bool),
		bottle:      make(chan *rhynock.Bottle),
	}

	// Register the route to rhynock handler function and pass in our BottleDst
	http.HandleFunc("/socket/", func(w http.ResponseWriter, r *http.Request) {
		rhynock.ConnectionHandler(w, r, router)
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		//http.ServeFile(w, r, "index.html")

		homeTempl := template.Must(template.ParseFiles("index.html"))
		homeTempl.Execute(w, r.Host)
	})

	// Start router listening routine
	go func() {
		for {
			// Listen for a bottle
			btl := <-router.bottle

			// Loop through all active connections
			for c, _ := range router.connections {
				// Send everyone the message
				c.Send <- btl.Message
			}
		}
	}()

	http.ListenAndServe(":8000", nil)
}
Ejemplo n.º 2
0
func main() {
	router := &Router{
		connections: make(map[*rhynock.Conn]*Profile),
		bottle:      make(chan *rhynock.Bottle),
	}

	// Register the route to rhynock handler function and pass in our BottleDst
	http.HandleFunc("/socket/", func(w http.ResponseWriter, r *http.Request) {
		rhynock.ConnectionHandler(w, r, router)
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, "index.html")
	})

	// Start router listening routine
	go func() {
		for {
			// Listen for a bottle
			btl := <-router.bottle

			// If this person is not authenticated forward the bottle to the login function
			if !router.connections[btl.Sender].authed {
				router.login(btl)
			} else {
				// If they are logged in...
				// reconstruct the message to include the username
				message := []byte(router.connections[btl.Sender].name + ": " + string(btl.Message))

				// Loop through all active connections
				for c, _ := range router.connections {
					// Send everyone the message
					c.Send <- message
				}
			}
		}
	}()

	http.ListenAndServe("127.0.0.1:8000", nil)
}