duan33360 2016-02-04 13:32
浏览 961
已采纳

Websocket握手失败404(golang服务器)

I have a simple go web server which serves on port localhost:8080 an public folder containing both an html file as well as a client script with websocket logic.

in my main.go file

listener, err := net.listen("tcp", "localhost:8080")
if err != nil {
    log.Fatal(err)
}
//full code in gist https://gist.github.com/Kielan/98706aaf5dc0be9d6fbe

then in my client script

try {
    var sock = new WebSocket("ws://127.0.0.1:8080");
    console.log("Websocket - status: " + sock.readyState);

    sock.onopen = function(message) {
    console.log("CONNECTION opened..." + this.readyState);
    //onmessage, onerr, onclose, ect...
}

I get the error in chrome

WebSocket connection to 'ws://127.0.0.1:8080/' failed: Error during WebSocket handshake: Unexpected response code: 200

and Firefox

Firefox can't establish a connection to the server at ws://127.0.0.1:8080/.

I found this article referring to node.js indicating to add /websocket to my client websocket string, though it did not solve the problem and resulted in a 404

I thought response code 200 is good, do I need to convert the request to a websocket somehow and maybe it is defaulting to http? If so how can I do this?

  • 写回答

1条回答 默认 最新

  • dongwei6457 2016-02-04 14:31
    关注

    Like JimB pointed out, you are not handling http nor websocket connections yet.

    You can do websocket handling with the package github.com/gorilla/websocket This is how a simple setup could look like:

    package main
    
    import (    
        "log"
        "net/http"
        "github.com/gorilla/websocket"
    )
    
    // wsHandler implements the Handler Interface
    type wsHandler struct{}
    
    func main() {
        router := http.NewServeMux()
        router.Handle("/", http.FileServer(http.Dir("./webroot"))) //handles static html / css etc. under ./webroot
        router.Handle("/ws", wsHandler{}) //handels websocket connections
    
        //serving
        log.Fatal(http.ListenAndServe("localhost:8080", router))
    }
    
    func (wsh wsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        // upgrader is needed to upgrade the HTTP Connection to a websocket Connection
            upgrader := &websocket.Upgrader{
                ReadBufferSize:  1024,
                WriteBufferSize: 1024,
            }
    
            //Upgrading HTTP Connection to websocket connection
            wsConn, err := upgrader.Upgrade(w, r, nil)
            if err != nil {
                log.Printf("error upgrading %s", err)
                return
            }
    
            //handle your websockets with wsConn
    }
    

    In your Javascript you then need var sock = new WebSocket("ws://localhost/ws:8080"); obviously.

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作