Skip to content

Commit

Permalink
Allow passing WebsocketUri to connect()
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Aug 22, 2019
1 parent ad127f0 commit 63499f4
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,28 @@ function connector(?Connector $connector = null): Connector
}

/**
* @param string|Handshake $handshake
* @param ConnectContext|null $connectContext
* @param CancellationToken|null $cancellationToken
* @param string|WebsocketUri|Handshake $handshake
* @param ConnectContext|null $connectContext
* @param CancellationToken|null $cancellationToken
*
* @return Promise<Connection>
*
* @throws \TypeError If $handshake is not a string or instance of \Amp\WebSocket\Handshake.
* @throws \TypeError If $handshake is not a string, instance of WebsocketUri, or instance of Handshake.
* @throws ConnectionException If the connection could not be established.
*/
function connect(
$handshake,
?ConnectContext $connectContext = null,
?CancellationToken $cancellationToken = null
): Promise {
if (\is_string($handshake)) {
if (\is_string($handshake) || $handshake instanceof WebsocketUri) {
$handshake = new Handshake($handshake);
} elseif (!$handshake instanceof Handshake) {
throw new \TypeError(\sprintf('Must provide an instance of %s or a websocket URL as a string', Handshake::class));
throw new \TypeError(\sprintf(
'Must provide an instance of %s or a websocket URL as a string or instance of %s',
Handshake::class,
WebsocketUri::class
));
}

return connector()->connect($handshake, $connectContext, $cancellationToken);
Expand Down

0 comments on commit 63499f4

Please sign in to comment.