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