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:
parent
7e42bfe2ac
commit
e3aecdf021
@ -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);
|
|
||||||
}
|
|
8
src/Ratchet/WebSocket/MessageCallableInterface.php
Normal file
8
src/Ratchet/WebSocket/MessageCallableInterface.php
Normal 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);
|
||||||
|
}
|
6
src/Ratchet/WebSocket/MessageComponentInterface.php
Normal file
6
src/Ratchet/WebSocket/MessageComponentInterface.php
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet\WebSocket;
|
||||||
|
use Ratchet\ComponentInterface;
|
||||||
|
|
||||||
|
interface MessageComponentInterface extends ComponentInterface, MessageCallableInterface {
|
||||||
|
}
|
@ -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);
|
||||||
|
@ -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) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user