Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(See also: <a href="https://stackoverflow.com/questions/8125507/how-can-i-send-and-receive-websocket-messages-on-the-server-side">How can I send and receive WebSocket messages on the server side?</a>)</p> <hr> <p>It's fairly easy, but it's important to understand the format.</p> <p>The first byte is almost always <code>1000 0001</code>, where the <code>1</code> means "last frame", the three <code>0</code>s are reserved bits without any meaning so far and the <code>0001</code> means that it's a text frame (which Chrome sends with the <code>ws.send()</code> method).</p> <p>(<strong>Update:</strong> Chrome can now also send binary frames with an <code>ArrayBuffer</code>. The last four bits of the first byte will be <code>0002</code>, so you can differ between text and binary data. The decoding of the data works exactly the same way.)</p> <p>The second byte contains of a <code>1</code> (meaning that it's "masked" (encoded)) followed by seven bits which represent the frame size. If it's between <code>000 0000</code> and <code>111 1101</code>, that's the size. If it's <code>111 1110</code>, the following 2 bytes are the length (because it wouldn't fit in seven bits), and if it's <code>111 1111</code>, the following 8 bytes are the length (if it wouldn't fit in two bytes either).</p> <p>Following that are four bytes which are the "masks" which you need to decode the frame data. This is done using xor encoding which uses one of the masks as defined by <code>indexOfByteInData mod 4</code> of the data. Decoding simply works like <code>encodedByte xor maskByte</code> (where <code>maskByte</code> is <code>indexOfByteInData mod 4</code>).</p> <p>Now I must say I'm not experienced with C# at all, but this is some pseudocode (some JavaScript accent I'm afraid):</p> <pre><code>var length_code = bytes[1] &amp; 127, // remove the first 1 by doing '&amp; 127' masks, data; if(length_code === 126) { masks = bytes.slice(4, 8); // 'slice' returns part of the byte array data = bytes.slice(8); // and accepts 'start' (inclusively) } else if(length_code === 127) { // and 'end' (exclusively) as arguments masks = bytes.slice(10, 14); // Passing no 'end' makes 'end' the length data = bytes.slice(14); // of the array } else { masks = bytes.slice(2, 6); data = bytes.slice(6); } // 'map' replaces each element in the array as per a specified function // (each element will be replaced with what is returned by the function) // The passed function accepts the value and index of the element as its // arguments var decoded = data.map(function(byte, index) { // index === 0 for the first byte return byte ^ masks[ index % 4 ]; // of 'data', not of 'bytes' // xor mod }); </code></pre> <p>You can also download <a href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17" rel="nofollow noreferrer">the specification</a> which can be helpful (it of course contains everything you need to understand the format).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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