From 560e5f78811f3403b890ad5bea17b20c63da08a6 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 23 Feb 2024 23:05:09 +0100 Subject: [PATCH] Bugfixed handling of fin flag in WebSocketConnection --- src/WebSocketConnection.php | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/WebSocketConnection.php b/src/WebSocketConnection.php index 5e4532b..b357aa5 100644 --- a/src/WebSocketConnection.php +++ b/src/WebSocketConnection.php @@ -18,6 +18,7 @@ class WebSocketConnection implements WebSocketInterface { use EventEmitterTrait; + // TODO maybe move constants to WebSocketProtocol? const OP_CONTINUATION = 0x0; const OP_FRAME_TEXT = 0x1; const OP_FRAME_BINARY = 0x2; @@ -58,10 +59,7 @@ class WebSocketConnection implements WebSocketInterface $this->groupManager = $groupManager; $this->inStream->on('data', $this->onWebSocketData(...)); - $this->inStream->on('close', function () { - $this->close(); - $this->emit('close', []); - }); + $this->inStream->on('close', $this->close(...)); } private function onWebSocketData($data) @@ -79,15 +77,15 @@ class WebSocketConnection implements WebSocketInterface } else { $this->buffer .= $payload; } + // Break out to avoid processing partial messages + return; } - if ($final) { + if ($this->bufferedOp !== null) { $payload = $this->buffer . $payload; $this->buffer = null; - if ($this->bufferedOp !== null) { - $opcode = $this->bufferedOp; - $this->bufferedOp = null; - } + $opcode = $this->bufferedOp; + $this->bufferedOp = null; } switch ($opcode) { @@ -257,15 +255,14 @@ class WebSocketConnection implements WebSocketInterface */ public function close() { - // TODO send close $this->outStream->close(); $this->inStream->close(); - // TODO emit close event + + $this->emit('close', []); } public function closeWithReason(string $reason, int $code=1000) { - // TODO send close $payload = chr(($code >> 8) & 0xFF) . chr($code & 0xFF) . $reason; $this->send(self::OP_CLOSE, $payload); } @@ -275,7 +272,7 @@ class WebSocketConnection implements WebSocketInterface */ public function end($data = null) { - + // TODO implement me } } \ No newline at end of file