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 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加