diff --git a/src/WebSocketCodec.php b/src/WebSocketCodec.php index ed7c18a..fab998a 100644 --- a/src/WebSocketCodec.php +++ b/src/WebSocketCodec.php @@ -17,6 +17,20 @@ use React\Stream\WritableStreamInterface; class WebSocketCodec { + /** + * Encode a frame. Required keys are opcode and payload. Keys that can be passed are: + * + * opcode (int) - the opcode + * final (bool) - final frame + * rsv1-3 (bool) - reserved bits + * masked (bool) - if the frame was masked + * mask (string) - the mask bytes, if masked + * length (int) - length of payload + * payload (string) - the payload + * + * @param array $frame The frame + * @return string The encoded frame + */ public function encode(array $frame): string { // Encoded frame @@ -82,21 +96,24 @@ class WebSocketCodec $encoded .= $frame['payload']; } - //$this->hexdump($encoded); - return $encoded; } /** * Decode a websocket frame and return an array with the keys: + * * opcode (int) - the opcode - * fin (bool) - final frame + * final (bool) - final frame * rsv1-3 (bool) - reserved bits * masked (bool) - if the frame was masked + * mask (string) - the mask bytes, if masked * length (int) - length of payload * payload (string) - the payload + * + * @param string $frame The frame data to decode + * @return array The decoded frame */ - public function decode($frame): array + public function decode(string $frame): array { // Decoded frame $decoded = []; diff --git a/src/WebSocketConnection.php b/src/WebSocketConnection.php index efac912..aa8ac17 100644 --- a/src/WebSocketConnection.php +++ b/src/WebSocketConnection.php @@ -25,8 +25,10 @@ class WebSocketConnection implements WebSocketInterface const OP_PING = 0x9; const OP_PONG = 0xA; + /** @var string|null The name of the group that this connection has joined, or null */ private ?string $groupName = null; + /** @var WebSocketCodec The frame encoder/decoder */ private WebSocketCodec $codec; private ?ConnectionGroup $group = null; @@ -171,32 +173,50 @@ class WebSocketConnection implements WebSocketInterface return $this->request->getServerParams()['SERVER_ADDR']; } + /** + * {@inheritDoc} + */ public function isReadable() { return $this->inStream->isReadable(); } + /** + * {@inheritDoc} + */ public function pause() { $this->inStream->pause(); } + /** + * {@inheritDoc} + */ public function resume() { $this->inStream->resume(); } + /** + * {@inheritDoc} + */ public function isWritable() { return $this->outStream->isWritable(); } + /** + * {@inheritDoc} + */ public function pipe(WritableStreamInterface $dest, array $options = array()) { // TODO implement return $dest; } + /** + * {@inheritDoc} + */ public function write($data) { return $this->send(self::OP_FRAME_TEXT, $data); @@ -225,12 +245,18 @@ class WebSocketConnection implements WebSocketInterface return true; } + /** + * {@inheritDoc} + */ public function close() { $this->outStream->close(); $this->inStream->close(); } + /** + * {@inheritDoc} + */ public function end($data = null) {