duan33360
duan33360
2016-02-04 13:32
浏览 345

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?

图片转代码服务由CSDN问答提供 功能建议

我有一个简单的Go Web服务器,该服务器在localhost:8080端口上提供了一个同时包含html文件的公用文件夹 作为具有websocket逻辑的客户端脚本。

在我的 main.go文件中

  listener,错误:= net.listen(“ tcp”,“ localhost:8080”)
if错误!= nil {
 log.Fatal(  err)
} 
 //要点中的完整代码https://gist.github.com/Kielan/98706aaf5dc0be9d6fbe
   
 
 

然后在我的客户端脚本中

  try {
 var sock = new WebSocket(“ ws://127.0.0.1:8080”); 
 console.log(“ Websocket-status:” + sock。  readyState); 
 
 sock.onopen = function(message){
 console.log(“ CONNECTION open ...” + this.readyState); 
 // onmessage,onerr,onclose,ect ... \  n} 
   
 
 

我收到了chrome中的错误

  WebSocket连接到'ws://127.0.0.1  :8080 /'失败:WebSocket握手期间出错:意外 响应代码:200 
   
 
 

和Firefox

  Firefox无法在ws建立与服务器的连接:  //127.0.0.1:8080 /。
   
 
 

我发现本文涉及到node.js,指示将/ websocket添加到我的客户端websocket字符串中,尽管它不能解决问题并导致404 < / p>

我认为响应代码200不错,是否需要以某种方式将请求转换为websocket,也许默认为http? 如果可以,我该怎么做?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwei6457
    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.

    点赞 评论

相关推荐