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

huangapple go评论141阅读模式
英文:

Why do I get the error message "http: response.Write on hijacked connection"?

问题

我正在尝试使用gorilla websocket库来了解Go语言中的websocket工作原理。但是,当我点击浏览器上的刷新按钮时,我一直收到这个错误消息。

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

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

第一个错误消息的状态码是"going away",我猜测是因为当我点击刷新按钮时,它会从websocket连接中断开,所以这对我来说是有道理的。

但是,我还收到了一个我不理解的错误消息,即"hijacked"。为什么会出现这个错误消息,它是什么意思?

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

我正在使用以下代码:

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
}

客户端代码:

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);

当我第一次加载页面时,控制台只输出"foo!"。所以总的来说,在加载页面一次后,再重新加载两次,我会得到如下输出:

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!

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

英文:

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?

答案1

得分: 4

websocket.Upgrade 函数会从 http.ResponseWriter 中劫持底层的网络连接。

当连接从 writer 中劫持后,ResponseWriter.Write 方法会在调用 write 后记录此消息。

看起来应用程序使用的路由器或中间件在 websocket 处理程序返回后仍然向连接写入数据。

英文:

The websocket.Upgrade function hijacks the the underlying network connection from the http.ResponseWriter.

The ResponseWriter.Write method logs this message when write is called after the connection is hijacked from the writer.

It looks like the router or middleware used by the application is writing to connection after the websocket handler returns.

huangapple
  • 本文由 发表于 2015年9月19日 01:13:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/32657603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定