Skip to content

Commit

Permalink
Merge branch 'uniquoooo'
Browse files Browse the repository at this point in the history
  • Loading branch information
cboden committed Mar 15, 2016
2 parents dc795c6 + 5d012a9 commit 7c39664
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
46 changes: 39 additions & 7 deletions src/WebSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class WebSocket implements EventEmitterInterface {
*/
protected $_stream;

/**
* @var \Closure
*/
protected $_close;

/**
* WebSocket constructor.
* @param \React\Stream\DuplexStreamInterface $stream
Expand All @@ -46,6 +51,18 @@ public function __construct(DuplexStreamInterface $stream, ResponseInterface $re
$this->response = $response;
$this->request = $request;

$self = $this;
$this->_close = function($code = null, $reason = null) use ($self) {
static $sent = false;

if ($sent) {
return;
}
$sent = true;

$self->emit('close', [$code, $reason, $self]);
};

$reusableUAException = new \UnderflowException;

$streamer = new MessageBuffer(
Expand All @@ -56,13 +73,26 @@ function(MessageInterface $msg) {
function(FrameInterface $frame) use (&$streamer) {
switch ($frame->getOpcode()) {
case Frame::OP_CLOSE:
$frameContents = $frame->getPayload();

$reason = '';
$code = unpack('n', substr($frameContents, 0, 2));
$code = reset($code);

if (($frameLen = strlen($frameContents)) > 2) {
$reason = substr($frameContents, 2, $frameLen);
}

$closeFn = $this->_close;
$closeFn($code, $reason);

return $this->_stream->end($streamer->newFrame($frame->getPayload(), true, Frame::OP_CLOSE)->maskPayload()->getContents());
case Frame::OP_PING:
return $this->send($streamer->newFrame($frame->getPayload(), true, Frame::OP_PONG));
case Frame::OP_PONG:
return $this->emit('pong', [$frame, $this]);
default:
return $this->_stream->end($streamer->newFrame(Frame::CLOSE_PROTOCOL, true, Frame::OP_CLOSE)->maskPayload()->getContents());
return $this->close(Frame::CLOSE_PROTOCOL);
}
},
false,
Expand All @@ -80,9 +110,7 @@ function() use ($reusableUAException) {
}
});

$stream->on('close', function() {
$this->emit('close', [$this]);
});
$stream->on('close', $this->_close);

$stream->on('error', function($error) {
$this->emit('error', [$error, $this]);
Expand All @@ -104,9 +132,13 @@ public function send($msg) {
$this->_stream->write($msg->getContents());
}

public function close($code = 1000) {
$frame = new Frame(pack('n', $code), true, Frame::OP_CLOSE);
public function close($code = 1000, $reason = '') {
$frame = new Frame(pack('n', $code) . $reason, true, Frame::OP_CLOSE);
$this->_stream->write($frame->getContents());

$closeFn = $this->_close;
$closeFn($code, $reason);

$this->_stream->end();
}
}
}
2 changes: 1 addition & 1 deletion tests/autobahn/runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require __DIR__ . '/../../vendor/autoload.php';

define('AGENT', 'Pawl/0.0.1');
define('AGENT', 'Pawl/0.2.1');

$loop = React\EventLoop\Factory::create();

Expand Down

0 comments on commit 7c39664

Please sign in to comment.