API documentation
This commit is contained in:
parent
07f5d49996
commit
01b51041d5
@ -2,7 +2,13 @@
|
|||||||
namespace Ratchet\Command;
|
namespace Ratchet\Command;
|
||||||
use Ratchet\SocketCollection;
|
use Ratchet\SocketCollection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close the connection to the sockets passed in the constructor
|
||||||
|
*/
|
||||||
class CloseConnection implements CommandInterface {
|
class CloseConnection implements CommandInterface {
|
||||||
|
/**
|
||||||
|
* @var SocketCollection
|
||||||
|
*/
|
||||||
protected $_sockets;
|
protected $_sockets;
|
||||||
|
|
||||||
public function __construct(SocketCollection $sockets) {
|
public function __construct(SocketCollection $sockets) {
|
||||||
@ -10,6 +16,8 @@ class CloseConnection implements CommandInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function execute() {
|
function execute() {
|
||||||
$this->_sockets->close();
|
foreach ($this->_sockets as $socket) {
|
||||||
|
$socket->close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,8 +2,18 @@
|
|||||||
namespace Ratchet\Command;
|
namespace Ratchet\Command;
|
||||||
use Ratchet\SocketCollection;
|
use Ratchet\SocketCollection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Socket implementation of the Command Pattern
|
||||||
|
* User created applications are to return a Command to the server for execution
|
||||||
|
*/
|
||||||
interface CommandInterface {
|
interface CommandInterface {
|
||||||
|
/**
|
||||||
|
* Pass the Sockets to execute the command on
|
||||||
|
*/
|
||||||
function __construct(SocketCollection $sockets);
|
function __construct(SocketCollection $sockets);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Server class will call the execution
|
||||||
|
*/
|
||||||
function execute();
|
function execute();
|
||||||
}
|
}
|
@ -2,6 +2,9 @@
|
|||||||
namespace Ratchet\Command;
|
namespace Ratchet\Command;
|
||||||
use Ratchet\SocketCollection;
|
use Ratchet\SocketCollection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Null pattern - execution does nothing, something needs to be passed back though
|
||||||
|
*/
|
||||||
class Null implements CommandInterface {
|
class Null implements CommandInterface {
|
||||||
public function __construct(SocketCollection $sockets) {
|
public function __construct(SocketCollection $sockets) {
|
||||||
}
|
}
|
||||||
|
@ -2,22 +2,43 @@
|
|||||||
namespace Ratchet\Command;
|
namespace Ratchet\Command;
|
||||||
use Ratchet\SocketCollection;
|
use Ratchet\SocketCollection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send text back to the client end of the socket(s)
|
||||||
|
*/
|
||||||
class SendMessage implements CommandInterface {
|
class SendMessage implements CommandInterface {
|
||||||
|
/**
|
||||||
|
* @var SocketCollection
|
||||||
|
*/
|
||||||
protected $_sockets;
|
protected $_sockets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
protected $_message = '';
|
protected $_message = '';
|
||||||
|
|
||||||
public function __construct(SocketCollection $sockets) {
|
public function __construct(SocketCollection $sockets) {
|
||||||
$this->_sockets = $sockets;
|
$this->_sockets = $sockets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The message to send to the socket(s)
|
||||||
|
* @param string
|
||||||
|
*/
|
||||||
public function setMessage($msg) {
|
public function setMessage($msg) {
|
||||||
$this->_message = (string)$msg;
|
$this->_message = (string)$msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message from setMessage()
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getMessage() {
|
public function getMessage() {
|
||||||
return $this->_message;
|
return $this->_message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \UnexpectedValueException if a message was not set with setMessage()
|
||||||
|
*/
|
||||||
public function execute() {
|
public function execute() {
|
||||||
if (empty($this->_message)) {
|
if (empty($this->_message)) {
|
||||||
throw new \UnexpectedValueException("Message is empty");
|
throw new \UnexpectedValueException("Message is empty");
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet;
|
namespace Ratchet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses internal php methods to fill an Exception class (no parameters required)
|
||||||
|
*/
|
||||||
class Exception extends \Exception {
|
class Exception extends \Exception {
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$int = socket_last_error();
|
$int = socket_last_error();
|
||||||
|
@ -1,18 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Logging;
|
namespace Ratchet\Logging;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A logger used by the server and extending applications for debugging info
|
||||||
|
*/
|
||||||
interface LoggerInterface {
|
interface LoggerInterface {
|
||||||
/**
|
/**
|
||||||
|
* Just an informational log
|
||||||
* @param string
|
* @param string
|
||||||
*/
|
*/
|
||||||
function note($msg);
|
function note($msg);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* A problem, but nothing too serious
|
||||||
* @param string
|
* @param string
|
||||||
*/
|
*/
|
||||||
function warning($msg);
|
function warning($msg);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Bad things have happened...
|
||||||
* @param string
|
* @param string
|
||||||
*/
|
*/
|
||||||
function error($msg);
|
function error($msg);
|
||||||
|
@ -2,6 +2,9 @@
|
|||||||
namespace Ratchet\Logging;
|
namespace Ratchet\Logging;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adapt the awesome Monolog Logger into the lowly Ratchet Logger
|
||||||
|
*/
|
||||||
class MonologAdapter extends Logger implements LoggerInterface {
|
class MonologAdapter extends Logger implements LoggerInterface {
|
||||||
/**
|
/**
|
||||||
* Maps to Monolog\Logger::addInfo
|
* Maps to Monolog\Logger::addInfo
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Logging;
|
namespace Ratchet\Logging;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends all logs into the void
|
||||||
|
* No one can hear you scream in /dev/null
|
||||||
|
*/
|
||||||
class NullLogger implements LoggerInterface {
|
class NullLogger implements LoggerInterface {
|
||||||
function note($msg) {
|
function note($msg) {
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,8 @@ use Ratchet\SocketInterface;
|
|||||||
use Ratchet\ReceiverInterface;
|
use Ratchet\ReceiverInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* The adapter to handle WebSocket requests/responses
|
||||||
|
* This is a mediator between the Server and your application to handle real-time messaging through a web browser
|
||||||
* @link http://ca.php.net/manual/en/ref.http.php
|
* @link http://ca.php.net/manual/en/ref.http.php
|
||||||
* @todo Make sure this works both ways (client/server) as stack needs to exist on client for framing
|
* @todo Make sure this works both ways (client/server) as stack needs to exist on client for framing
|
||||||
* @todo Clean up Client/Version stuff. This should be a factory making single instances of Version classes, implement chain of reponsibility for version - client should implement an interface?
|
* @todo Clean up Client/Version stuff. This should be a factory making single instances of Version classes, implement chain of reponsibility for version - client should implement an interface?
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Protocol\Websocket;
|
namespace Ratchet\Protocol\WebSocket;
|
||||||
use Ratchet\ReceiverInterface;
|
use Ratchet\ReceiverInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,19 +2,22 @@
|
|||||||
namespace Ratchet\Protocol\WebSocket;
|
namespace Ratchet\Protocol\WebSocket;
|
||||||
use Ratchet\Protocol\WebSocket\Version\VersionInterface;
|
use Ratchet\Protocol\WebSocket\Version\VersionInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A representation of a Socket connection of the user on the other end of the socket
|
||||||
|
*/
|
||||||
class Client {
|
class Client {
|
||||||
/**
|
/**
|
||||||
* @type Ratchet\Protocol\WebSocket\Version\VersionInterface
|
* @var Ratchet\Protocol\WebSocket\Version\VersionInterface
|
||||||
*/
|
*/
|
||||||
protected $_version = null;
|
protected $_version = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $_hands_shook = false;
|
protected $_hands_shook = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param VersionInterface
|
* @param Version\VersionInterface
|
||||||
* @return Client
|
* @return Client
|
||||||
*/
|
*/
|
||||||
public function setVersion(VersionInterface $version) {
|
public function setVersion(VersionInterface $version) {
|
||||||
@ -23,7 +26,7 @@ class Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return VersionInterface
|
* @return Version\VersionInterface
|
||||||
*/
|
*/
|
||||||
public function getVersion() {
|
public function getVersion() {
|
||||||
return $this->_version;
|
return $this->_version;
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Protocol\WebSocket\Version;
|
namespace Ratchet\Protocol\WebSocket\Version;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Hixie76 is currently implemented by Safari
|
||||||
|
* Not yet complete
|
||||||
|
*/
|
||||||
class Hixie76 implements VersionInterface {
|
class Hixie76 implements VersionInterface {
|
||||||
public function handshake(array $headers) {
|
public function handshake(array $headers) {
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Protocol\WebSocket\Version;
|
namespace Ratchet\Protocol\WebSocket\Version;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The HyBi-10 version, identified in the headers as version 8, is currently implemented by the latest Chrome and Firefix version
|
||||||
|
* @link http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10
|
||||||
|
*/
|
||||||
class Hybi10 implements VersionInterface {
|
class Hybi10 implements VersionInterface {
|
||||||
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
||||||
|
|
||||||
/**
|
|
||||||
*/
|
|
||||||
public function handshake(array $headers) {
|
public function handshake(array $headers) {
|
||||||
$key = $this->sign($headers['Sec-Websocket-Key']);
|
$key = $this->sign($headers['Sec-Websocket-Key']);
|
||||||
|
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Protocol\WebSocket\Version;
|
namespace Ratchet\Protocol\WebSocket\Version;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Despite the version iterations of WebInterface the actions they go through are similar
|
||||||
|
* This standardizes how the server handles communication with each protocol version
|
||||||
|
*/
|
||||||
interface VersionInterface {
|
interface VersionInterface {
|
||||||
/**
|
/**
|
||||||
* Perform the handshake and return the response headers
|
* Perform the handshake and return the response headers
|
||||||
|
@ -4,6 +4,7 @@ use Ratchet\Server;
|
|||||||
use Ratchet\SocketObserver;
|
use Ratchet\SocketObserver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Decorator interface for internal protocols
|
||||||
* @todo Should probably move this into \Ratchet\Server namespace
|
* @todo Should probably move this into \Ratchet\Server namespace
|
||||||
*/
|
*/
|
||||||
interface ReceiverInterface extends SocketObserver {
|
interface ReceiverInterface extends SocketObserver {
|
||||||
|
@ -6,6 +6,7 @@ use Ratchet\Logging\LoggerInterface;
|
|||||||
use Ratchet\Logging\NullLogger;
|
use Ratchet\Logging\NullLogger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Creates an open-ended socket to listen on a port for incomming connections. Events are delegated through this to attached applications
|
||||||
* @todo Consider using _connections as master reference and passing iterator_to_array(_connections) to socket_select
|
* @todo Consider using _connections as master reference and passing iterator_to_array(_connections) to socket_select
|
||||||
* @todo Move SocketObserver methods to separate class, create, wrap class in __construct
|
* @todo Move SocketObserver methods to separate class, create, wrap class in __construct
|
||||||
* @todo Currently passing Socket object down the decorated chain - should be sending reference to it instead; Receivers do not interact with the Socket directly, they do so through the Command pattern
|
* @todo Currently passing Socket object down the decorated chain - should be sending reference to it instead; Receivers do not interact with the Socket directly, they do so through the Command pattern
|
||||||
@ -48,7 +49,6 @@ class Server implements SocketObserver, \IteratorAggregate {
|
|||||||
/**
|
/**
|
||||||
* @param Ratchet\Socket
|
* @param Ratchet\Socket
|
||||||
* @param ReceiverInterface
|
* @param ReceiverInterface
|
||||||
* @param boolean True, enables debug mode and the server doesn't infiniate loop
|
|
||||||
* @param Logging\LoggerInterface
|
* @param Logging\LoggerInterface
|
||||||
*/
|
*/
|
||||||
public function __construct(SocketInterface $host, ReceiverInterface $application, LoggerInterface $logger = null) {
|
public function __construct(SocketInterface $host, ReceiverInterface $application, LoggerInterface $logger = null) {
|
||||||
@ -128,8 +128,8 @@ class Server implements SocketObserver, \IteratorAggregate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @param mixed
|
* @param mixed The address to listen for incoming connections on. "0.0.0.0" to listen from anywhere
|
||||||
* @param int
|
* @param int The port to listen to connections on
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
* @todo Validate address. Use socket_get_option, if AF_INET must be IP, if AF_UNIX must be path
|
* @todo Validate address. Use socket_get_option, if AF_INET must be IP, if AF_UNIX must be path
|
||||||
* @todo Should I make handling open/close/msg an application?
|
* @todo Should I make handling open/close/msg an application?
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet;
|
namespace Ratchet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
interface ServerInterface extends \IteratorAggregate {
|
interface ServerInterface extends \IteratorAggregate {
|
||||||
function attatchReceiver(ReceiverInterface $receiver);
|
function attatchReceiver(ReceiverInterface $receiver);
|
||||||
|
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet;
|
namespace Ratchet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A self-deprecating queue that can only hold Socket objects
|
||||||
|
*/
|
||||||
class SocketCollection extends \SplQueue {
|
class SocketCollection extends \SplQueue {
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
// parent::__construct();
|
|
||||||
$this->setIteratorMode(static::IT_MODE_DELETE);
|
$this->setIteratorMode(static::IT_MODE_DELETE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param SocketInterface
|
||||||
|
*/
|
||||||
public function enqueue(SocketInterface $value) {
|
public function enqueue(SocketInterface $value) {
|
||||||
parent::enqueue($value);
|
parent::enqueue($value);
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,27 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet;
|
namespace Ratchet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An object-oriented container for a single socket connection
|
||||||
|
*/
|
||||||
interface SocketInterface {
|
interface SocketInterface {
|
||||||
/**
|
/**
|
||||||
|
* Send text to the client on the other end of the socket
|
||||||
* @param string
|
* @param string
|
||||||
* @param int
|
* @param int
|
||||||
*/
|
*/
|
||||||
function write($buffer, $length = 0);
|
function write($buffer, $length = 0);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Called when the client sends data to the server through the socket
|
||||||
* @param string
|
* @param string
|
||||||
* @param int
|
* @param int
|
||||||
* @param int
|
* @param int
|
||||||
*/
|
*/
|
||||||
function recv(&$buf, $len, $flags);
|
function recv(&$buf, $len, $flags);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close the open connection to the client/socket
|
||||||
|
*/
|
||||||
function close();
|
function close();
|
||||||
}
|
}
|
@ -1,19 +1,25 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet;
|
namespace Ratchet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Observable/Observer design pattern interface for handing events on a socket
|
||||||
|
*/
|
||||||
interface SocketObserver {
|
interface SocketObserver {
|
||||||
/**
|
/**
|
||||||
|
* When a new connection is opened it will be passed to this method
|
||||||
* @param SocketInterface
|
* @param SocketInterface
|
||||||
*/
|
*/
|
||||||
function onOpen(SocketInterface $conn);
|
function onOpen(SocketInterface $conn);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Triggered when a client sends data through the socket
|
||||||
* @param SocketInterface
|
* @param SocketInterface
|
||||||
* @param string
|
* @param string
|
||||||
*/
|
*/
|
||||||
function onRecv(SocketInterface $from, $msg);
|
function onRecv(SocketInterface $from, $msg);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* This is called just before the connection is closed
|
||||||
* @param SocketInterface
|
* @param SocketInterface
|
||||||
*/
|
*/
|
||||||
function onClose(SocketInterface $conn);
|
function onClose(SocketInterface $conn);
|
||||||
|
Loading…
Reference in New Issue
Block a user