// Copyright 2015 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. // +build ignore package main import ( "flag" "log" "net/http" "github.com/gorilla/websocket" ) var addr = flag.String("addr", "localhost:8081", "http service address") var upgrader = websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { return true }, } /* use default options, except origin. Allows cross-origin requests for testing. Presents a security risk if run in production */ func echo(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.Error(w, "Not found", 404) return } if r.Method != "GET" { http.Error(w, "Method not allowed", 405) return } c, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Print("upgrade:", err) return } defer c.Close() for { mt, message, err := c.ReadMessage() if err != nil { log.Println("read:", err) break } log.Printf("recv: %s", message) err = c.WriteMessage(mt, message) if err != nil { log.Println("write:", err) break } } } func main() { flag.Parse() log.SetFlags(0) http.HandleFunc("/", echo) err := http.ListenAndServe(*addr, nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }