Skip to content

Commit

Permalink
#69: Remove large payload on false ret (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman authored Jul 26, 2022
1 parent 826c6c7 commit f04fa59
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/WebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,14 @@ private function acceptNewClient($server, array &$readSocks): void
*/
private function messagesWorker(array $readSocks): void
{
$largePayloadData = '';
foreach ($readSocks as $kSock => $sock) {
$data = $this->decode(fread($sock, self::MAX_BYTES_READ));
if ($data !== null) {
$dataType = $data['type'];
$dataPayload = $data['payload'];

if ($data === false) { // payload is too large - waiting for remained data
$largePayloadData += $dataPayload;
$dataType = null;
$dataPayload = null;
if ($data !== false) { // payload is too large - waiting for remained data
$dataType = $data['type'];
$dataPayload = $data['payload'];
}

// to manipulate connection through send/close methods via handler, specified in IConnection
Expand All @@ -261,11 +260,6 @@ private function messagesWorker(array $readSocks): void
&& method_exists($this->handler, self::MAP_EVENT_TYPE_TO_METHODS[$dataType]);
if ($isSupportedMethod) {
try {
if (empty($largePayloadData) === false) {
$dataPayload = $largePayloadData + $dataPayload;
$largePayloadData = ''; // clear saved large payload for the next one
}

// dynamic call: onMessage, onPing, onPong
$this->handler->{self::MAP_EVENT_TYPE_TO_METHODS[$dataType]}($cureentConn, $dataPayload);
} catch (WebSocketException $e) {
Expand All @@ -289,7 +283,7 @@ private function handshake($client, string $headers): string
$match = [];
preg_match(self::SEC_WEBSOCKET_KEY_PTRN, $headers, $match);
if (empty($match[1])) {
return false;
return '';
}

$key = $match[1];
Expand Down

0 comments on commit f04fa59

Please sign in to comment.