dongxiangxie8181 2017-10-02 06:56
浏览 111
已采纳

如何在Go中将Websocket与NewServeMux结合使用?

I got an error message: cannot use websocket.Handler(handler) (type websocket.Handler) as type func(http.ResponseWriter, *http.Request) in argument to mx.HandleFunc

in following code

package main

import (
    "golang.org/x/net/websocket"
    "net/http"
)

func handler(ws *websocket.Conn) {
    // TODO
}
func main() {
    mx := http.NewServeMux()
    mx.HandleFunc("/ws", websocket.Handler(handler)) // error

    http.ListenAndServe("localhost:8888", mx)
}
  • 写回答

1条回答 默认 最新

  • dtdfl62844 2017-10-02 07:16
    关注

    I believe you should use mx.Handle, not mx.HandleFunc

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?