123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package main
- import (
- "fmt"
- // "io"
- "log"
- "net/http"
- // "github.com/gorilla/websocket"
- )
- func homePage(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, "Home Page")
- }
- func serveWs(pool *Pool, w http.ResponseWriter, r *http.Request) {
- fmt.Println("WebSocket Endpoint Hit")
- conn, err := Upgrade(w, r)
- if err != nil {
- fmt.Fprintf(w, "%+v\n", err)
- }
- if (conn == nil) {
- log.Println("Client not using websocket protocol")
- fmt.Fprintf(w, "%+v\n", err)
- return
- }
- client := &Client{
- Conn: conn,
- Pool: pool,
- }
- pool.Register <- client
- client.Read()
- }
- func setupRoutes() {
- http.HandleFunc("/", homePage)
- pool := NewPool()
- go pool.Start()
- http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
- serveWs(pool, w, r)
- })
- }
- func main() {
- fmt.Println("Distributed Chat App v0.01")
- setupRoutes()
- log.Fatal(http.ListenAndServe(":8008", nil))
- }
|