Bugfixed handling of fin flag in WebSocketConnection

This commit is contained in:
Chris 2024-02-23 23:05:09 +01:00
parent 6f45622b09
commit 560e5f7881

View File

@ -18,6 +18,7 @@ class WebSocketConnection implements WebSocketInterface
{ {
use EventEmitterTrait; use EventEmitterTrait;
// TODO maybe move constants to WebSocketProtocol?
const OP_CONTINUATION = 0x0; const OP_CONTINUATION = 0x0;
const OP_FRAME_TEXT = 0x1; const OP_FRAME_TEXT = 0x1;
const OP_FRAME_BINARY = 0x2; const OP_FRAME_BINARY = 0x2;
@ -58,10 +59,7 @@ class WebSocketConnection implements WebSocketInterface
$this->groupManager = $groupManager; $this->groupManager = $groupManager;
$this->inStream->on('data', $this->onWebSocketData(...)); $this->inStream->on('data', $this->onWebSocketData(...));
$this->inStream->on('close', function () { $this->inStream->on('close', $this->close(...));
$this->close();
$this->emit('close', []);
});
} }
private function onWebSocketData($data) private function onWebSocketData($data)
@ -79,16 +77,16 @@ class WebSocketConnection implements WebSocketInterface
} else { } else {
$this->buffer .= $payload; $this->buffer .= $payload;
} }
// Break out to avoid processing partial messages
return;
} }
if ($final) { if ($this->bufferedOp !== null) {
$payload = $this->buffer . $payload; $payload = $this->buffer . $payload;
$this->buffer = null; $this->buffer = null;
if ($this->bufferedOp !== null) {
$opcode = $this->bufferedOp; $opcode = $this->bufferedOp;
$this->bufferedOp = null; $this->bufferedOp = null;
} }
}
switch ($opcode) { switch ($opcode) {
case self::OP_PING: case self::OP_PING:
@ -257,15 +255,14 @@ class WebSocketConnection implements WebSocketInterface
*/ */
public function close() public function close()
{ {
// TODO send close
$this->outStream->close(); $this->outStream->close();
$this->inStream->close(); $this->inStream->close();
// TODO emit close event
$this->emit('close', []);
} }
public function closeWithReason(string $reason, int $code=1000) public function closeWithReason(string $reason, int $code=1000)
{ {
// TODO send close
$payload = chr(($code >> 8) & 0xFF) . chr($code & 0xFF) . $reason; $payload = chr(($code >> 8) & 0xFF) . chr($code & 0xFF) . $reason;
$this->send(self::OP_CLOSE, $payload); $this->send(self::OP_CLOSE, $payload);
} }
@ -275,7 +272,7 @@ class WebSocketConnection implements WebSocketInterface
*/ */
public function end($data = null) public function end($data = null)
{ {
// TODO implement me
} }
} }