title: JavaScript WebSocket non-null-terminated strings
body: <p>I have a very strange issue. I have an open WebSocket, the communication works perfectly. I also have onblur and onfocus events for the window, which notify the server via said connection. However, in that case, the strings I receive are non-null-terminated. The communication works absolutely impeccably otherwise, even when sending the same strings that are sent on those respective blur-/focus-events. Why is that, and how can it be fixed?</p>
  
  <p>Here's some code:</p>
  
  <pre><code>$(document).ready(function(){
  
      initializeEverything();
  
      window.onblur = function(){ notifyFocusChange(false); };
  
      window.onfocus = function(){ notifyFocusChange(true); };
  
  });
  
  function notifyFocusChange(present){
  
      if(present){
          webSocket.send('presence:present');
      }else{
          webSocket.send('presence:absent');
      }
  
  }
  </code></pre>
  
  <p>And here's an example of a non-null-terminated string I receive when the events fire:</p>
  
  <p><code>presence:absentÏ┘ê÷/à°äJ÷ÝÿLÓ▓ùM÷Ýÿ[</code></p>
  
  <p>EDIT: It's been suggested that it may be a server error, so here's the code for decoding incoming messages:</p>
  
  <pre><code>private function decode($payload) {
  
      $length = ord($payload[1]) &amp; 127;
  
      if ($length == 126) {
          $masks = substr($payload, 4, 4);
          $data = substr($payload, 8);
      } elseif ($length == 127) {
          $masks = substr($payload, 10, 4);
          $data = substr($payload, 14);
      } else {
          $masks = substr($payload, 2, 4);
          $data = substr($payload, 6);
      }
  
      $text = '';
      for ($i = 0; $i &lt; strlen($data); ++$i) {
          $text .= $data[$i] ^ $masks[$i % 4];
      }
  
      return $text;
  
  }
  </code></pre>
  
  <p>EDIT: It only happens in Chrome. I checked it on Firefox, which uses the same WebSocket-protocol, and everything works perfectly fine there.</p>