英文:
Websocket output received by browser, complains about "Invalid UTF-8 sequence in header value "
问题
当我在浏览器中加载页面时,页面被正确地提供。当javascript执行时,Chrome的控制台输出显示:
Invalid UTF-8 sequence in header value
我已经搜索了这个字符串,但无法找到任何关于golang的提及。
我该如何告诉golang不要将Unicode字符写入web sockets?
我认为这是问题的原因,因为“网络”选项卡只显示了一个空的请求和响应。
CCSSE:
main.go:
package main
import (
"fmt"
"net/http"
"log"
"code.google.com/p/go.net/websocket"
//"github.com/garyburd/go-websocket/websocket"
)
const listenAddress = "localhost:9999"
func wsHandler(webSck *websocket.Conn) {
fmt.Fprint(webSck, "Rpy")
fmt.Println("Sent \"Rpy\" to web socket", webSck)
//more code here
}
func main() {
http.Handle("/", http.FileServer(http.Dir("./static")))
http.Handle("/ws", websocket.Handler(wsHandler))
err := http.ListenAndServe(listenAddress, nil)
if err != nil {
log.Fatal(err)
}
}
static/main.js
var socket = new WebSocket("ws://localhost:9999/ws");
socket.onopen = function() {
socket.onmessage = function(m) {
console.log("Received: " + m);
};
socket.send("Req\n");
};
编辑:
如@Intermernet建议的,我已经设置了Sec-WebSocket-Protocol
头。但是仍然得到Invalid UTF-8 sequence in header value
的错误。
还要注意的是,我需要执行webSck.Config().Header = make(http.Header)
的原因是它是nil
- 通过对webSck.Config()
的日志语句进行确认。再加上另一个问题 - 为什么我需要这样做;是否有我错过的初始化步骤?
func wsHandler(webSck *websocket.Conn) {
webSck.Config().Header = make(http.Header)
webSck.Config().Header.Add("Sec-WebSocket-Protocol", "chat")
fmt.Printf("ws.Config() %#v\n", webSck.Config())
var buf []byte;
buf = []byte("Rpy")
_, err := webSck.Write(buf)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Printf("Sent \"Rpy\" to web socket %#v\n", webSck)
}
}
英文:
When I load the page in my browser, the page gets served correctly. When the javascript executes, Chrome's console output says:
Invalid UTF-8 sequence in header value
I have searched for that string, and am unable to find any mention of it for golang.
How do I go about telling golang not to write unicode characters to web sockets?
I assume that this is the cause of the problem, as the "Network" tab only reveals an empty request and response for this.
CCSSE:
main.go:
package main
import (
"fmt"
"net/http"
"log"
"code.google.com/p/go.net/websocket"
//"github.com/garyburd/go-websocket/websocket"
)
const listenAddress = "localhost:9999"
func wsHandler(webSck *websocket.Conn) {
fmt.Fprint(webSck, "Rpy")
fmt.Println("Sent \"Rpy\" to web socket", webSck)
//more code here
}
func main() {
http.Handle("/", http.FileServer(http.Dir("./static")))
http.Handle("/ws", websocket.Handler(wsHandler))
err := http.ListenAndServe(listenAddress, nil)
if err != nil {
log.Fatal(err)
}
}
static/main.js
var socket = new WebSocket("ws://localhost:9999/ws");
socket.onopen = function() {
socket.onmessage = function(m) {
console.log("Received: " + m);
};
socket.send("Req\n");
};
EDIT:
As suggested by @Intermernet, I have set the Sec-WebSocket-Protocol
header. To no avail, still getting Invalid UTF-8 sequence in header value
.
Note also that the reason I need to do webSck.Config().Header = make(http.Header)
is that it is nil
- confirmed by the log statement on webSck.Config()
. Tack on to this another question - why do I have to do this; is there an intialisation step that I have missed somewhere?
func wsHandler(webSck *websocket.Conn) {
webSck.Config().Header = make(http.Header)
webSck.Config().Header.Add("Sec-WebSocket-Protocol", "chat")
fmt.Printf("ws.Config() %#v\n", webSck.Config())
var buf []byte;
buf = []byte("Rpy")
_, err := webSck.Write(buf)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Printf("Sent \"Rpy\" to web socket %#v\n", webSck)
}
}
答案1
得分: 2
这个问题是否与code.google.com/p/go.net/websocket的最近更改有关?
https://code.google.com/p/go/source/detail?r=1e65ca1b2499c473ec267ca1d6759b3dc920a599&repo=net
英文:
Can this issue be related to recent change in code.google.com/p/go.net/websocket ?
https://code.google.com/p/go/source/detail?r=1e65ca1b2499c473ec267ca1d6759b3dc920a599&repo=net
答案2
得分: 0
抱歉以回答的形式发表评论,我还不能发表评论...
您是否在websocket配置中设置了Sec-WebSocket-Protocol头?(https://code.google.com/p/go/source/browse/websocket/websocket.go?repo=net#74和https://www.rfc-editor.org/rfc/rfc6455#page-59)
这个错误消息似乎在Google上出现了很多次,与该头部的不兼容或冲突有关。
可以尝试像这样:
func wsHandler(webSck *websocket.Conn) {
webSck.Config.Header.Add("Sec-WebSocket-Protocol", "chat")
fmt.Fprint(webSck, "Rpy")
fmt.Println("已发送\"Rpy\"到web socket", webSck)
//更多代码
}
另外,我认为最好使用websocket的Write函数(https://code.google.com/p/go/source/browse/websocket/websocket.go?repo=net#205),因为它将写入的数据封装/转换为帧。
所以更像是这样:
func wsHandler(webSck *websocket.Conn) {
webSck.Config.Header.Add("Sec-WebSocket-Protocol", "chat")
_, _ = webSck.Write("Rpy")
fmt.Println("已发送\"Rpy\"到web socket")
//更多代码
}
但是当然要检查Write的错误!
希望能有所帮助。
编辑:从fmt.Println中删除多余的", webSck"。
英文:
Sorry for the comment in the form of an answer, I can't comment yet...
Are you setting Sec-WebSocket-Protocol header on the websocket Config? (https://code.google.com/p/go/source/browse/websocket/websocket.go?repo=net#74 and https://www.rfc-editor.org/rfc/rfc6455#page-59)
That error message seems to crop up on Google many times relating to incompatibility or conflict with that header.
Something like:
func wsHandler(webSck *websocket.Conn) {
webSck.Config.Header.Add("Sec-WebSocket-Protocol", "chat")
fmt.Fprint(webSck, "Rpy")
fmt.Println("Sent \"Rpy\" to web socket", webSck)
//more code here
}
Also, I think it's preferred to use the Write function on the websocket (https://code.google.com/p/go/source/browse/websocket/websocket.go?repo=net#205) as it encapsulates/converts the written data into frames.
So more like:
func wsHandler(webSck *websocket.Conn) {
webSck.Config.Header.Add("Sec-WebSocket-Protocol", "chat")
_, _ = webSck.Write("Rpy")
fmt.Println("Sent \"Rpy\" to web socket")
//more code here
}
But obviously checking the error from Write!
Hope that helps.
EDIT: Remove the extraneous ", webSck" from the fmt.Println .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论