Alternative approach to binary messaging

A new interface the dev can implement that will pass a
Message object to the devs instance. The object has
properties regarding binary/text
This commit is contained in:
Chris Boden 2016-03-01 14:11:15 -05:00
parent 7e42bfe2ac
commit e3aecdf021
5 changed files with 52 additions and 17 deletions

View File

@ -1,8 +0,0 @@
<?php
namespace Ratchet\WebSocket;
use Ratchet\MessageInterface;
use Ratchet\ConnectionInterface;
interface BinaryMessageInterface extends MessageInterface {
public function onMessage(ConnectionInterface $conn, $msg, $isBinary = false);
}

View File

@ -0,0 +1,8 @@
<?php
namespace Ratchet\WebSocket;
use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\MessageInterface;
interface MessageCallableInterface {
public function onMessage(ConnectionInterface $conn, MessageInterface $msg);
}

View File

@ -0,0 +1,6 @@
<?php
namespace Ratchet\WebSocket;
use Ratchet\ComponentInterface;
interface MessageComponentInterface extends ComponentInterface, MessageCallableInterface {
}

View File

@ -1,7 +1,8 @@
<?php <?php
namespace Ratchet\WebSocket; namespace Ratchet\WebSocket;
use Ratchet\MessageComponentInterface; use Ratchet\ComponentInterface;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface as DataComponentInterface;
use Ratchet\Http\HttpServerInterface; use Ratchet\Http\HttpServerInterface;
use Ratchet\Http\CloseResponseTrait; use Ratchet\Http\CloseResponseTrait;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
@ -26,7 +27,7 @@ class WsServer implements HttpServerInterface {
/** /**
* Decorated component * Decorated component
* @var \Ratchet\MessageComponentInterface * @var \Ratchet\ComponentInterface
*/ */
private $delegate; private $delegate;
@ -56,10 +57,27 @@ class WsServer implements HttpServerInterface {
private $pongReceiver; private $pongReceiver;
/** /**
* @param \Ratchet\MessageComponentInterface $component Your application to run with WebSockets * @var \Closure
* If you want to enable sub-protocols have your component implement WsServerInterface as well
*/ */
public function __construct(MessageComponentInterface $component) { private $msgCb;
/**
* @param \Ratchet\WebSocket\MessageComponentInterface|\Ratchet\MessageComponentInterface $component Your application to run with WebSockets
* @note If you want to enable sub-protocols have your component implement WsServerInterface as well
*/
public function __construct(ComponentInterface $component) {
if ($component instanceof MessageComponentInterface) {
$this->msgCb = function(ConnectionInterface $conn, MessageInterface $msg) {
$this->delegate->onMessage($conn, $msg);
};
} elseif ($component instanceof DataComponentInterface) {
$this->msgCb = function(ConnectionInterface $conn, MessageInterface $msg) {
$this->delegate->onMessage($conn, $msg->getPayload());
};
} else {
throw new \UnexpectedValueException('Expected instance of \Ratchet\WebSocket\MessageComponentInterface or \Ratchet\MessageComponentInterface');
}
$this->delegate = $component; $this->delegate = $component;
$this->connections = new \SplObjectStorage; $this->connections = new \SplObjectStorage;
@ -106,7 +124,8 @@ class WsServer implements HttpServerInterface {
$streamer = new MessageBuffer( $streamer = new MessageBuffer(
$this->closeFrameChecker, $this->closeFrameChecker,
function(MessageInterface $msg) use ($wsConn) { function(MessageInterface $msg) use ($wsConn) {
$this->delegate->onMessage($wsConn, $msg->getPayload(), $msg->isBinary()); $cb = $this->msgCb;
$cb($wsConn, $msg);
}, },
function(FrameInterface $frame) use ($wsConn) { function(FrameInterface $frame) use ($wsConn) {
$this->onControlFrame($frame, $wsConn); $this->onControlFrame($frame, $wsConn);

View File

@ -1,10 +1,20 @@
<?php <?php
use Ratchet\ConnectionInterface;
require dirname(dirname(dirname(__DIR__))) . '/vendor/autoload.php'; require dirname(dirname(dirname(__DIR__))) . '/vendor/autoload.php';
class BinaryEcho extends \Ratchet\Server\EchoServer implements \Ratchet\WebSocket\BinaryMessageInterface { class BinaryEcho implements \Ratchet\WebSocket\MessageComponentInterface {
public function onMessage(\Ratchet\ConnectionInterface $from, $msg, $isBinary = false) { public function onMessage(ConnectionInterface $from, \Ratchet\RFC6455\Messaging\MessageInterface $msg) {
$from->send($msg, $isBinary); $from->send($msg);
}
public function onOpen(ConnectionInterface $conn) {
}
public function onClose(ConnectionInterface $conn) {
}
public function onError(ConnectionInterface $conn, \Exception $e) {
} }
} }