websocket/examples/chat/main.go

41 lines
884 B
Go
Raw Normal View History

// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
2013-10-16 20:41:47 +04:00
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"log"
"net/http"
)
var addr = flag.String("addr", ":8080", "http service address")
func serveHome(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL)
2013-10-16 20:41:47 +04:00
if r.URL.Path != "/" {
2018-02-26 06:30:15 +03:00
http.Error(w, "Not found", http.StatusNotFound)
2013-10-16 20:41:47 +04:00
return
}
if r.Method != "GET" {
2018-02-26 06:30:15 +03:00
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
2013-10-16 20:41:47 +04:00
return
}
http.ServeFile(w, r, "home.html")
2013-10-16 20:41:47 +04:00
}
func main() {
flag.Parse()
hub := newHub()
go hub.run()
2013-10-16 20:41:47 +04:00
http.HandleFunc("/", serveHome)
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(hub, w, r)
})
2013-10-16 20:41:47 +04:00
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}