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 关注
- 147 浏览
添加回答
举报