Skip to content

Commit

Permalink
Adding support of custom functions within protocols.
Browse files Browse the repository at this point in the history
Adding support of WebSocket subprotocols for WebSocket client.

Usage example:

$worker->onWorkerStart = function() {
    $ws = new AsyncTcpConnection("ws://192.168.1.16:1884/");
    $ws->WSSetProtocol('mqtt');
    $ws->onWebSocketConnect = function($conn) {
        echo "WebSocketConnection is set, server protocol is: [".$conn->WSGetServerProtocol()."]\n";
};
  • Loading branch information
vponomarev committed Oct 19, 2017
1 parent b61b82f commit 796c413
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Connection/TcpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
20 changes: 20 additions & 0 deletions Protocols/Ws.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 796c413

Please sign in to comment.