1 回答

TA貢獻1891條經驗 獲得超3個贊
函數的第二個參數類型http.ListenAndServe是http.Handler,如果他為nil,http lib使用http.DefaultServeMuxhttp.Handler。
你的/api路由注冊到mux.NewRouter(),你的/路由/ws注冊到http.DefaultServeMux,這是兩個不同的http.Handler objetc,你需要合并兩個路由器注冊的路由請求。
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/api", home)
// move ws up, prevent '/*' from covering '/ws' in not testing mux, httprouter has this bug.
router.HandleFunc("/ws", handleConnections)
// PathPrefix("/") match '/*' request
router.PathPrefix("/").Handler(http.FileServer(http.Dir("../public")))
go handleMessages()
http.ListenAndServe(":8000", router)
gorilla/mux 示例不使用 http.HandleFunc 函數。
- 1 回答
- 0 關注
- 159 瀏覽
添加回答
舉報