diff --git a/Connection/TcpConnection.php b/Connection/TcpConnection.php index ba7ca760f..200894ebc 100644 --- a/Connection/TcpConnection.php +++ b/Connection/TcpConnection.php @@ -254,6 +254,31 @@ class TcpConnection extends ConnectionInterface self::STATUS_CLOSED => 'CLOSED', ); + + /** + * Adding support of custom functions within protocols + * + * @param string $name + * @param array $arguments + */ + public function __call($name, $arguments) { + // Try to emit custom function within protocol + if (method_exists($this->protocol, $name)) { + try { + return call_user_func(array($this->protocol, $name), $this, $arguments); + } catch (\Exception $e) { + Worker::log($e); + exit(250); + } catch (\Error $e) { + Worker::log($e); + exit(250); + } + } else { + trigger_error('Call to undefined method '.__CLASS__.'::'.$name.'()', E_USER_ERROR); + } + + } + /** * Construct. * diff --git a/Protocols/Ws.php b/Protocols/Ws.php index 43605f36b..99fcc1d95 100644 --- a/Protocols/Ws.php +++ b/Protocols/Ws.php @@ -374,6 +374,7 @@ public static function sendHandshake($connection) "Connection: Upgrade\r\n". "Upgrade: websocket\r\n". "Origin: ". (isset($connection->websocketOrigin) ? $connection->websocketOrigin : '*') ."\r\n". + (isset($connection->WSClientProtocol)?"Sec-WebSocket-Protocol: ".$connection->WSClientProtocol."\r\n":''). "Sec-WebSocket-Version: 13\r\n". "Sec-WebSocket-Key: " . base64_encode(md5(mt_rand(), true)) . "\r\n\r\n"; $connection->send($header, true); @@ -395,6 +396,16 @@ public static function dealHandshake($buffer, $connection) $pos = strpos($buffer, "\r\n\r\n"); if ($pos) { // handshake complete + + // Get WebSocket subprotocol (if specified by server) + $header = explode("\r\n", substr($buffer, 0, $pos)); + foreach ($header as $hrow) { + if (preg_match("#^(.+?)\:(.+?)$#", $hrow, $m) && ($m[1] == "Sec-WebSocket-Protocol")) { + $connection->WSServerProtocol = trim($m[2]); + } + + } + $connection->handshakeStep = 2; $handshake_response_length = $pos + 4; // Try to emit onWebSocketConnect callback. @@ -430,4 +441,13 @@ public static function dealHandshake($buffer, $connection) } return 0; } + + public static function WSSetProtocol($connection, $params) { + $connection->WSClientProtocol = $params[0]; + } + + public static function WSGetServerProtocol($connection) { + return (property_exists($connection, 'WSServerProtocol')?$connection->WSServerProtocol:null); + } + }