package mainimport ("fmt""github.com/gorilla/websocket""log""net/http"
)
var upgrader = websocket.Upgrader{ReadBufferSize: 1024,WriteBufferSize: 1024,CheckOrigin: func(r *http.Request) bool { return true },
}
func reader(conn *websocket.Conn) {for {messageType, p, err := conn.ReadMessage()if err != nil {log.Println(err)return}str := fmt.Sprintf("%s %s", p, p)fmt.Println(str)if err := conn.WriteMessage(messageType, []byte(str)); err != nil {log.Println(err)return}}
}
func serveWs(w http.ResponseWriter, r *http.Request) {fmt.Println(r.Host)ws, err := upgrader.Upgrade(w, r, nil)if err != nil {log.Println(err)}reader(ws)
}func setupRoutes() {http.HandleFunc("/ws", serveWs)
}func main() {setupRoutes()http.ListenAndServe(":8080", nil)
}