Discussion:
sending binary websocket frames
germanium-0UDz38MK/
2013-01-04 11:20:37 UTC
Permalink
Hi, I'm writing a websocket-to-socket proxy for the spice html5 client
(http://www.spice-space.org/page/Html5) using this websocket package:
http://godoc.org/code.google.com/p/go.net/websocket
I'm using it just like in the Handler example in the doc, using Read and
Write on the *websocket.Conn (I'm really new to websockets).

The spice html5 client requires the binary subprotocol and it fails upon
the first encounter of a text frame (opcode 0x1).
The client handshake contains "Sec-WebSocket-Protocol: binary" but the
server handshake doesn't (meaning that it doesn't agree on it) and all my
writings to the *websocket.Conn become text frames.
How do I make them binary frames (opcode 0x2)?

Setting ws.Config().Protocol to []string{"binary"} in the Handler has no
effect (ws being the *websocket.Conn).

from code.google.com/p/go/source/browse/websocket/server.go:
15 config := new(Config)
16 var hs serverHandshaker = &hybiServerHandshaker{Config: config}
...
41 config.Protocol = nil
42
43 err = hs.AcceptHandshake(buf.Writer)

I can see that AcceptHandshake will always find hs.Protocol == nil,
therefore it will never write a Sec-WebSocket-Protocol field. Is it
intended to never agree on the client requested protocol? Am I missing
something?
I don't seem to have problems decoding binary frames from the client.

Thanks,
Leo III

--
Donovan Hide
2013-01-04 11:59:14 UTC
Permalink
Post by germanium-0UDz38MK/
The spice html5 client requires the binary subprotocol and it fails upon
the first encounter of a text frame (opcode 0x1).
The client handshake contains "Sec-WebSocket-Protocol: binary" but the
server handshake doesn't (meaning that it doesn't agree on it) and all my
writings to the *websocket.Conn become text frames.
How do I make them binary frames (opcode 0x2)?
Are you definitely sending []byte Messages rather than string?

http://code.google.com/p/go/source/browse/websocket/websocket.go?repo=net#338

--
germanium-0UDz38MK/
2013-01-04 14:17:07 UTC
Permalink
Post by Donovan Hide
Are you definitely sending []byte Messages rather than string?
http://code.google.com/p/go/source/browse/websocket/websocket.go?repo=net#338
Thanks for your quick reply and sorry for not noticing these obvious things.
Yes, I was sending []byte, but not with Send, I was using Write, like this:
ws.Write(data).
Doing it like this: websocket.Message.Send(ws, data), it sends binary
frames as expected.
Write probably uses the payload type set in ws.PayloadType (the doc doesn't
say anything about it but the name is quite explicative), I succeeded in
sending binary frames with Write after setting it to 0x2.
Anyway, I still don't know why the server always set config.Protocol to nil
after reading the client handshake
(http://code.google.com/p/go/source/browse/websocket/server.go?repo=net#41),
making it useless for the AcceptHandshake method to check it
(http://code.google.com/p/go/source/browse/websocket/hybi.go?repo=net#534).
What if I want the server to write a Sec-WebSocket-Protocol field in
response?

Thanks,
Leo III

--

Loading...