dooid3005
2015-09-18 17:13
浏览 710
已采纳

为什么会收到错误消息“ http:response.Write on hijacked connection”?

I am trying out the gorilla websocket library to get a feel of how websockets work with Go. But I keep getting this error message when I hit refresh button on the browser.

When I reload the web page that I am using to test the websocket, I get these error messages on the Go console:

2015/09/18 19:04:41 websocket: close 1001
2015/09/18 19:04:41 http: response.Write on hijacked connection

The first one is status code for "going away". I am assuming it is because when I hit refresh it goes away from the websocket connection so that makes sense to me.

But then I get an error message that I don't understand. The hijacked one. Why do I get it and what does it mean?

I am running my code on localhost:8080 on a windows machine.

The code I am using:

func wsHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) error {
    conn, err := websocket.Upgrade(w, r, nil, 1024, 1024)
    if err != nil {
        return err
    }
    defer conn.Close()

    for {
        _, msg, err := conn.ReadMessage()
        if err != nil {
            return err
        }
        log.Println(string(msg))
    }
    return nil
}

Client side:

var conn = new WebSocket("ws://localhost:8080/api/messages/websocket");

conn.onclose = function (e) {
    console.log("onclose fired");
};

conn.onopen = function (e) {
    console.log("onopen fired");
};

conn.onmessage = function (e) {
    console.log(e.data);
};              

setTimeout(function () {
    conn.send("foo!");
}, 1500);

When I load the page first time, only foo! is outputted to the console. So all in all, after loading the page once, and then reloading it twice I get an output like this:

2015/09/18 19:04:39 foo!
2015/09/18 19:04:41 websocket: close 1001
2015/09/18 19:04:41 http: response.Write on hijacked connection
2015/09/18 19:04:43 foo!
2015/09/18 19:04:44 websocket: close 1001
2015/09/18 19:04:44 http: response.Write on hijacked connection
2015/09/18 19:04:46 foo!

What does this mean? I'm I doing something wrong?

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

我正在尝试使用大猩猩websocket库,以了解websocket如何与Go一起使用。 但是当我点击浏览器上的“刷新”按钮时,我仍然收到此错误消息。

当我重新加载用于测试websocket的网页时,我会在Go上收到这些错误消息 控制台:

  2015/09/18 19:04:41 websocket:关闭1001 
2015 / 09/18 19:04:41 http:响应。在被劫持的连接上写入\  n   
 
 

第一个是“离开”的状态码。 我以为是因为当我点击刷新时,它脱离了websocket连接,所以对我来说很有意义。

但是随后我收到了一条我不理解的错误消息。 被劫持的人。 为什么我得到它,它是什么意思?

我正在Windows机器上的localhost:8080上运行我的代码。

我的代码 正在使用:

  func wsHandler(w http.ResponseWriter,r * http.Request,_ httprouter.Params)error {
 conn,err:= websocket.Upgrade(w  ,r,nil,1024,1024)
如果err!= nil {
返回err 
} 
延迟conn.Close()
 
为{
 _,msg,err:= conn.ReadMessage  ()
 if err!= nil {
 return err 
} 
 log.Println(string(msg))
} 
 return nil 
} 
   
  
 

客户端:

  var conn = new WebSocket(“ ws:// localhost:8080 / api / messages / websocket”); 
 
conn。  onclose =函数(e){
 console.log(“ onclose fired”); 
}; 
 
conn.onopen =函数(e){
 console.log(“ onopen fired”); 
}  ; 
 
conn.onmessage =函数(e){
 console.log(e.data); 
};  
 
setTimeout(function(){
 conn.send(“ foo!”); 
},1500); 
   
 
 

当我加载页面时 第一次,只有 foo!输出到控制台。 因此,总的来说,一次加载页面,然后重新加载两次,我得到的输出是这样的:

  2015/09/18 19:04:39 foo!\  n2015 / 09/18 19:04:41 websocket:关闭1001 
2015 / 09/18 19:04:41 http:response。写在被劫持的连接上
2015 / 09/18 19:04:43 foo!
2015 / 09  / 18 19:04:44 websocket:关闭1001 
2015 / 09/18 19:04:44 http:响应。在被劫持的连接上写入
2015 / 09/18 19:04:46 foo!
  <  / pre> 
 
 

是什么意思? 我做错什么了?

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

1条回答 默认 最新

相关推荐 更多相似问题