Skip to content

Commit

Permalink
Update Handshake
Browse files Browse the repository at this point in the history
Handshake is now a value-object. The constructor now accepts Ws instances or a string.
  • Loading branch information
trowski committed Feb 10, 2019
1 parent 20e65bc commit 6a9af60
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"require": {
"php": ">=7.1",
"amphp/amp": "^2",
"amphp/http": "dev-master as 1.0.1",
"amphp/http": "dev-master as 1.1",
"amphp/byte-stream": "^1",
"amphp/socket": "^0.10",
"amphp/websocket": "dev-master as 1.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// Connects to the websocket endpoint in examples/broadcast-server/server.php provided in
// amphp/websocket-server (https://github.com/amphp/websocket-server).
Amp\Loop::run(function () {
$handshake = new Handshake('ws://localhost:1337/broadcast');
$handshake->setHeader('Origin', 'http://localhost:1337');
$handshake = (new Handshake('ws://localhost:1337/broadcast'))
->withHeader('Origin', 'http://localhost:1337');

/** @var Connection $connection */
$connection = yield connect($handshake);
Expand Down
2 changes: 1 addition & 1 deletion examples/kaazing-echo-demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

Amp\Loop::run(function () {
/** @var Connection $connection */
$connection = yield connect('ws://demos.kaazing.com/echo');
$connection = yield connect('wss://demos.kaazing.com/echo');
yield $connection->send('Hello!');

$i = 0;
Expand Down
52 changes: 45 additions & 7 deletions src/Handshake.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,27 @@ final class Handshake extends Message
private $options;

/**
* @param string $uri target address of websocket (e.g. ws://foo.bar/bar or
* wss://crypto.example/?secureConnection)
* @param string|Ws $uri target address of websocket (e.g. ws://foo.bar/bar or
* wss://crypto.example/?secureConnection) or a \League\Uri\Ws instance.
* @param Options|null $options
* @param string[]|string[][] $headers
*
* @throws \TypeError If $uri is not a string or and instance of \League\Uri\Ws.
* @throws \Error If compression is enabled in the options but the zlib extension is not installed.
*/
public function __construct(string $uri, ?Options $options = null, array $headers = [])
public function __construct($uri, ?Options $options = null, array $headers = [])
{
try {
$this->uri = Ws::createFromString($uri);
} catch (UriException $exception) {
throw new \Error('Invalid Websocket URI provided', 0, $exception);
if (\is_string($uri)) {
try {
$uri = Ws::createFromString($uri);
} catch (UriException $exception) {
throw new \Error('Invalid Websocket URI provided', 0, $exception);
}
} elseif (!$uri instanceof Ws) {
throw new \TypeError(\sprintf('Must provide an instance of %s or a URL as a string', Ws::class));
}

$this->uri = $uri;
$this->options = $options ?? new Options;

if ($this->options->isCompressionEnabled() && !\extension_loaded('zlib')) {
Expand All @@ -57,4 +63,36 @@ public function getOptions(): Options
{
return $this->options;
}

public function withHeaders(array $headers): self
{
$clone = clone $this;
$clone->setHeaders($headers);

return $clone;
}

public function withHeader(string $name, $value): self
{
$clone = clone $this;
$clone->setHeader($name, $value);

return $clone;
}

public function withAddedHeader(string $name, $value): self
{
$clone = clone $this;
$clone->addHeader($name, $value);

return $clone;
}

public function withoutHeader(string $name): self
{
$clone = clone $this;
$clone->removeHeader($name);

return $clone;
}
}

0 comments on commit 6a9af60

Please sign in to comment.