Exemplo n.º 1
0
 public static function fromSocketData(WebSocketSocket $socket, $data)
 {
     $headers = WebSocketFunctions::parseHeaders($data);
     if (isset($headers['Sec-Websocket-Key1'])) {
         $s = new WebSocketConnectionHixie($socket, $headers, $data);
         $s->sendHandshakeResponse();
     } else {
         if (strpos($data, '<policy-file-request/>') === 0) {
             $s = new WebSocketConnectionFlash($socket, $data);
         } else {
             $s = new WebSocketConnectionHybi($socket, $headers);
             $s->sendHandshakeResponse();
         }
     }
     return $s;
 }
Exemplo n.º 2
0
 /**
  * TODO: Proper header generation!
  * TODO: Check server response!
  */
 public function open()
 {
     $errno = $errstr = null;
     $protocol = $this->scheme == 'ws' ? "tcp" : "ssl";
     $this->socket = stream_socket_client("{$protocol}://{$this->host}:{$this->port}", $errno, $errstr, $this->getTimeOut());
     // socket_connect($this->socket, $this->host, $this->port);
     $buffer = $this->serializeHeaders();
     fwrite($this->socket, $buffer, strlen($buffer));
     // wait for response
     $buffer = WebSocketFunctions::readWholeBuffer($this->socket);
     $headers = WebSocketFunctions::parseHeaders($buffer);
     $s = new WebSocketSocket($this, $this->socket, $immediateWrite = true);
     if ($this->hybi) {
         $this->_connection = new WebSocketConnectionHybi($s, $headers);
     } else {
         $this->_connection = new WebSocketConnectionHixie($s, $headers, $buffer);
     }
     $s->setConnection($this->_connection);
     return true;
 }