New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Binary support #244
Binary support #244
Conversation
@@ -131,6 +131,7 @@ function Socket(uri, opts){ | |||
this.upgrade = false !== opts.upgrade; | |||
this.path = (opts.path || '/engine.io').replace(/\/$/, '') + '/'; | |||
this.forceJSONP = !!opts.forceJSONP; | |||
this.forceBase64 = !!opts.forceBase64; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should document this
Thanks for the comments. I'll make the style changes. I totally agree about the test. I'll look into the feature detecting binary support for WebSockets. When it comes to base64 encoding, it is actually currently automatically decoded and handed in as binary if binary types are supported. Maybe this didn't come across that well from my documentation. I'll make that better. |
…n there is no binaryType. If WebSocket doesn't support binary, this is communicated to the server through the upgrade packet.
…upgrade packet for WebSocket
… support binary types
…g to a transport that doesn't support binary from one that does to avoid the overhead of base64 encoding
This pull request answers to the issue of sending binary data over engine.io. Please take a look at the engine.io pull request for more details about binary on different transport methods.
With these changes binary data can be sent from the server to the client, and the other way around. The API for sending binary data is the same as for sending utf-8 strings, but binary types are detected. When a client on a browser calls
socket.send
for anArrayBuffer
, or anArrayBufferView
the data is sent as binary. For a node server the possibilities are the aforementioned, and alsoBuffer
. The data is then received as binary on the other end.On a browser client, the data is by default received as an
ArrayBuffer
, but can be changed toBlob
via settingsocket.binaryType = 'blob'
. If Blob is not supported, the default type is used. On a Node client the default type isBuffer
, but can be set toArrayBuffer
withsocket.binaryType = 'arraybuffer'
. IfArrayBuffer
is not supported on browsers, then an object like this is returned:{ base64: true, data: data }
, where data contains the binary data as a base64 string.The server needs to know when a client doesn't support XHR2 for binary data, so the client notifies the server of this by adding a b64 query parameter to the handshaking request. This will lead to binary messages travelling base64 encoded, even when using XHR polling.