github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/gorilla/websocket/examples/chat/main.go (about) 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 "text/template" 12 ) 13 14 var addr = flag.String("addr", ":8080", "http service address") 15 var homeTempl = template.Must(template.ParseFiles("home.html")) 16 17 func serveHome(w http.ResponseWriter, r *http.Request) { 18 if r.URL.Path != "/" { 19 http.Error(w, "Not found", 404) 20 return 21 } 22 if r.Method != "GET" { 23 http.Error(w, "Method not allowed", 405) 24 return 25 } 26 w.Header().Set("Content-Type", "text/html; charset=utf-8") 27 homeTempl.Execute(w, r.Host) 28 } 29 30 func main() { 31 flag.Parse() 32 go h.run() 33 http.HandleFunc("/", serveHome) 34 http.HandleFunc("/ws", serveWs) 35 err := http.ListenAndServe(*addr, nil) 36 if err != nil { 37 log.Fatal("ListenAndServe: ", err) 38 } 39 }