More inline docs

This commit is contained in:
Chris 2024-02-22 00:34:11 +01:00
parent 417c11670a
commit be6955ea48
2 changed files with 47 additions and 4 deletions

View File

@ -17,6 +17,20 @@ use React\Stream\WritableStreamInterface;
class WebSocketCodec 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 public function encode(array $frame): string
{ {
// Encoded frame // Encoded frame
@ -82,21 +96,24 @@ class WebSocketCodec
$encoded .= $frame['payload']; $encoded .= $frame['payload'];
} }
//$this->hexdump($encoded);
return $encoded; return $encoded;
} }
/** /**
* Decode a websocket frame and return an array with the keys: * Decode a websocket frame and return an array with the keys:
*
* opcode (int) - the opcode * opcode (int) - the opcode
* fin (bool) - final frame * final (bool) - final frame
* rsv1-3 (bool) - reserved bits * rsv1-3 (bool) - reserved bits
* masked (bool) - if the frame was masked * masked (bool) - if the frame was masked
* mask (string) - the mask bytes, if masked
* length (int) - length of payload * length (int) - length of payload
* payload (string) - the 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 frame
$decoded = []; $decoded = [];

View File

@ -25,8 +25,10 @@ class WebSocketConnection implements WebSocketInterface
const OP_PING = 0x9; const OP_PING = 0x9;
const OP_PONG = 0xA; const OP_PONG = 0xA;
/** @var string|null The name of the group that this connection has joined, or null */
private ?string $groupName = null; private ?string $groupName = null;
/** @var WebSocketCodec The frame encoder/decoder */
private WebSocketCodec $codec; private WebSocketCodec $codec;
private ?ConnectionGroup $group = null; private ?ConnectionGroup $group = null;
@ -171,32 +173,50 @@ class WebSocketConnection implements WebSocketInterface
return $this->request->getServerParams()['SERVER_ADDR']; return $this->request->getServerParams()['SERVER_ADDR'];
} }
/**
* {@inheritDoc}
*/
public function isReadable() public function isReadable()
{ {
return $this->inStream->isReadable(); return $this->inStream->isReadable();
} }
/**
* {@inheritDoc}
*/
public function pause() public function pause()
{ {
$this->inStream->pause(); $this->inStream->pause();
} }
/**
* {@inheritDoc}
*/
public function resume() public function resume()
{ {
$this->inStream->resume(); $this->inStream->resume();
} }
/**
* {@inheritDoc}
*/
public function isWritable() public function isWritable()
{ {
return $this->outStream->isWritable(); return $this->outStream->isWritable();
} }
/**
* {@inheritDoc}
*/
public function pipe(WritableStreamInterface $dest, array $options = array()) public function pipe(WritableStreamInterface $dest, array $options = array())
{ {
// TODO implement // TODO implement
return $dest; return $dest;
} }
/**
* {@inheritDoc}
*/
public function write($data) public function write($data)
{ {
return $this->send(self::OP_FRAME_TEXT, $data); return $this->send(self::OP_FRAME_TEXT, $data);
@ -225,12 +245,18 @@ class WebSocketConnection implements WebSocketInterface
return true; return true;
} }
/**
* {@inheritDoc}
*/
public function close() public function close()
{ {
$this->outStream->close(); $this->outStream->close();
$this->inStream->close(); $this->inStream->close();
} }
/**
* {@inheritDoc}
*/
public function end($data = null) public function end($data = null)
{ {