Created
July 23, 2019 20:26
-
-
Save mowings/9599bf7806573e3fa61f9076cc846bce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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