OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / gorilla / websocket / examples / chat / main.go
diff --git a/vendor/github.com/gorilla/websocket/examples/chat/main.go b/vendor/github.com/gorilla/websocket/examples/chat/main.go
new file mode 100644 (file)
index 0000000..9d4737a
--- /dev/null
@@ -0,0 +1,40 @@
+// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
+// 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)
+       if r.URL.Path != "/" {
+               http.Error(w, "Not found", http.StatusNotFound)
+               return
+       }
+       if r.Method != "GET" {
+               http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
+               return
+       }
+       http.ServeFile(w, r, "home.html")
+}
+
+func main() {
+       flag.Parse()
+       hub := newHub()
+       go hub.run()
+       http.HandleFunc("/", serveHome)
+       http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
+               serveWs(hub, w, r)
+       })
+       err := http.ListenAndServe(*addr, nil)
+       if err != nil {
+               log.Fatal("ListenAndServe: ", err)
+       }
+}