Skip to content

Commit 6c1f81a

Browse files
authored
Merge pull request #4 from CharlotteDunoisLabs/close-code
Properly do close code and reason
2 parents f0a06bc + 6fbe608 commit 6c1f81a

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

src/WebSocketConnection.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,17 @@ function (Frame $frame) {
3232
$this->stream->write((new Frame($frame->getPayload(), true, Frame::OP_PONG))->getContents());
3333
return;
3434
case Frame::OP_CLOSE:
35-
$closeCode = 1000;
36-
if ($frame->getPayloadLength() >= 2) {
37-
list($closeCode) = array_merge(unpack('n*', substr($frame->getPayload(), 0, 2)));
38-
}
39-
40-
$this->stream->end($frame->getContents());
35+
$closeCode = unpack('n*', substr($frame->getPayload(), 0, 2));
36+
$closeCode = reset($closeCode) ?: 1000;
37+
$reason = '';
4138

42-
if ($closeCode >= 2000) {
43-
// emit close code as error
44-
$exception = new \Exception('WebSocket closed with code ' . $closeCode);
45-
$this->emit('error', [$exception, $this]);
46-
return;
39+
if ($frame->getPayloadLength() > 2) {
40+
$reason = substr($frame->getPayload(), 2);
4741
}
4842

49-
$this->emit('close', [$closeCode, $this]);
43+
$this->stream->end($frame->getContents());
5044

45+
$this->emit('close', [$closeCode, $this, $reason]);
5146
return;
5247
}
5348
},

0 commit comments

Comments
 (0)