OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / gorilla / websocket / examples / chat / main.go
1 // Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package main
6
7 import (
8         "flag"
9         "log"
10         "net/http"
11 )
12
13 var addr = flag.String("addr", ":8080", "http service address")
14
15 func serveHome(w http.ResponseWriter, r *http.Request) {
16         log.Println(r.URL)
17         if r.URL.Path != "/" {
18                 http.Error(w, "Not found", http.StatusNotFound)
19                 return
20         }
21         if r.Method != "GET" {
22                 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
23                 return
24         }
25         http.ServeFile(w, r, "home.html")
26 }
27
28 func main() {
29         flag.Parse()
30         hub := newHub()
31         go hub.run()
32         http.HandleFunc("/", serveHome)
33         http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
34                 serveWs(hub, w, r)
35         })
36         err := http.ListenAndServe(*addr, nil)
37         if err != nil {
38                 log.Fatal("ListenAndServe: ", err)
39         }
40 }