From 9d769768dc31a99bc9bda164004d73a98d0d2936 Mon Sep 17 00:00:00 2001 From: arthurkushman Date: Sun, 20 Jun 2021 14:20:47 +0200 Subject: [PATCH] #58: Fix large payload unsaved and closed conn --- src/WebSocketServer.php | 10 ++++++++++ 1 file changed, 10 insertions(+) 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) {