Note that there are some explanatory texts on larger screens.

plurals
  1. POWebsocket output received by browser, complains about "Invalid UTF-8 sequence in header value "
    text
    copied!<p>When I load the page in my browser, the page gets served correctly. When the javascript executes, Chrome's console output says:</p> <pre><code>Invalid UTF-8 sequence in header value </code></pre> <p>I have searched for that string, and am unable to find any mention of it for golang.</p> <p>How do I go about telling golang not to write unicode characters to web sockets?</p> <p>I assume that this is the cause of the problem, as the "Network" tab only reveals an empty request and response for this.</p> <hr> <p>CCSSE:</p> <p>main.go:</p> <pre><code>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) } } </code></pre> <p>static/main.js</p> <pre><code>var socket = new WebSocket("ws://localhost:9999/ws"); socket.onopen = function() { socket.onmessage = function(m) { console.log("Received: " + m); }; socket.send("Req\n"); }; </code></pre> <hr> <p>EDIT:</p> <p>As suggested by @Intermernet, I have set the <code>Sec-WebSocket-Protocol</code> header. To no avail, still getting <code>Invalid UTF-8 sequence in header value</code>.</p> <p>Note also that the reason I need to do <code>webSck.Config().Header = make(http.Header)</code> is that it is <code>nil</code> - confirmed by the log statement on <code>webSck.Config()</code>. Tack on to this another question - why do I have to do this; is there an intialisation step that I have missed somewhere?</p> <pre><code>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) } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload