Merge branch '0.4-wip/binary-alternative' into 0.4
This commit is contained in:
commit
a5aed61626
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;
|
||||||
|
|
||||||
@ -105,7 +123,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);
|
$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,13 +1,29 @@
|
|||||||
<?php
|
<?php
|
||||||
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
||||||
require dirname(dirname(dirname(__DIR__))) . '/vendor/autoload.php';
|
require dirname(dirname(dirname(__DIR__))) . '/vendor/autoload.php';
|
||||||
|
|
||||||
|
class BinaryEcho implements \Ratchet\WebSocket\MessageComponentInterface {
|
||||||
|
public function onMessage(ConnectionInterface $from, \Ratchet\RFC6455\Messaging\MessageInterface $msg) {
|
||||||
|
$from->send($msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onOpen(ConnectionInterface $conn) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onClose(ConnectionInterface $conn) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onError(ConnectionInterface $conn, \Exception $e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$port = $argc > 1 ? $argv[1] : 8000;
|
$port = $argc > 1 ? $argv[1] : 8000;
|
||||||
$impl = sprintf('React\EventLoop\%sLoop', $argc > 2 ? $argv[2] : 'StreamSelect');
|
$impl = sprintf('React\EventLoop\%sLoop', $argc > 2 ? $argv[2] : 'StreamSelect');
|
||||||
|
|
||||||
$loop = new $impl;
|
$loop = new $impl;
|
||||||
$sock = new React\Socket\Server($loop);
|
$sock = new React\Socket\Server($loop);
|
||||||
$app = new Ratchet\Http\HttpServer(new Ratchet\WebSocket\WsServer(new Ratchet\Server\EchoServer));
|
$app = new Ratchet\Http\HttpServer(new Ratchet\WebSocket\WsServer(new BinaryEcho));
|
||||||
|
|
||||||
$sock->listen($port, '0.0.0.0');
|
$sock->listen($port, '0.0.0.0');
|
||||||
|
|
||||||
|
@ -7,6 +7,6 @@
|
|||||||
]
|
]
|
||||||
|
|
||||||
, "cases": ["*"]
|
, "cases": ["*"]
|
||||||
, "exclude-cases": ["1.2.*", "2.3", "2.4", "2.6", "9.2.*", "9.4.*", "9.6.*", "9.8.*"]
|
, "exclude-cases": []
|
||||||
, "exclude-agent-cases": {}
|
, "exclude-agent-cases": {}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user