Skip to content

Instantly share code, notes, and snippets.

@mowings
Created July 23, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mowings/9599bf7806573e3fa61f9076cc846bce to your computer and use it in GitHub Desktop.
Save mowings/9599bf7806573e3fa61f9076cc846bce to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"golang.org/x/net/websocket"
"net/http"
"log"
)
// Обычный путь для httprouter.
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
// Путь, по которому обрабатываются вебсокеты.
func Websocket(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
websocket.Handler(EchoHandler).ServeHTTP(w, r)
}
// Обработчик соединения вебсокета.
func EchoHandler(conn *websocket.Conn) {
io.Copy(conn, conn)
}
func main() {
router := httprouter.New()
router.GET("/", Index)
router.GET("/websocket", Websocket)
log.Fatal(http.ListenAndServe(":8080", router))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment