diff --git a/src/WebSocketServer.php b/src/WebSocketServer.php index 14ec085..daf5e21 100755 --- a/src/WebSocketServer.php +++ b/src/WebSocketServer.php @@ -242,12 +242,17 @@ 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; + } + // to manipulate connection through send/close methods via handler, specified in IConnection $cureentConn = new Connection($sock, $this->clients); if (empty($data) || $dataType === self::EVENT_TYPE_CLOSE) { // close event triggered from client - browser tab or close socket event @@ -267,6 +272,11 @@ 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) {