func server() { http.Handle("/", websocket.Handler(echoServer)) if err := http.ListenAndServe(":8000", nil); err != nil { log.Fatalf("http.ListenAndServe: %v", err) } }
// This method handles all requests. It dispatches to handleInternal after // handling / adapting websocket connections. func handle(w http.ResponseWriter, r *http.Request) { if maxRequestSize := int64(Config.IntDefault("http.maxrequestsize", 0)); maxRequestSize > 0 { r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize) } upgrade := r.Header.Get("Upgrade") if upgrade == "websocket" || upgrade == "Websocket" { websocket.Handler(func(ws *websocket.Conn) { //Override default Read/Write timeout with sane value for a web socket request ws.SetDeadline(time.Now().Add(time.Hour * 24)) r.Method = "WS" handleInternal(w, r, ws) }).ServeHTTP(w, r) } else { handleInternal(w, r, nil) } }