第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在同一個應用程序中使用 gorilla websocket 和 mux?

如何在同一個應用程序中使用 gorilla websocket 和 mux?

Go
藍山帝景 2022-05-23 18:08:53
func main() {    router := mux.NewRouter().StrictSlash(true)    router.HandleFunc("/api", home)    fs := http.FileServer(http.Dir("../public"))    http.Handle("/", fs)    http.HandleFunc("/ws", handleConnections)    go handleMessages()    log.Println("http server started on :8000")    err := http.ListenAndServe(":8000", nill)    if err != nil {        log.Fatal("ListenAndServe: ", err)    }}使用上面的代碼, /api 路由會給出 404。但是如果我將 更改err := http.ListenAndServe(":8000", nill)為err := http.ListenAndServe(":8000", router), /api 路由會起作用,但 / 路由(我服務于前端)會給出 404。我如何讓它們都工作?編輯:完整代碼 - https://codeshare.io/2Kpyb8
查看完整描述

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 函數。


查看完整回答
反對 回復 2022-05-23
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號