[WebSocket] Message refactoring
Moved the message buffering into its own class
This commit is contained in:
parent
3041b82971
commit
ee40816fe4
37
MessageParser.php
Normal file
37
MessageParser.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet\WebSocket;
|
||||||
|
|
||||||
|
class MessageParser {
|
||||||
|
public function onData(WsConnection $from, $data) {
|
||||||
|
if (!isset($from->WebSocket->message)) {
|
||||||
|
$from->WebSocket->message = $from->WebSocket->version->newMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
// There is a frame fragment attatched to the connection, add to it
|
||||||
|
if (!isset($from->WebSocket->frame)) {
|
||||||
|
$from->WebSocket->frame = $from->WebSocket->version->newFrame();
|
||||||
|
}
|
||||||
|
|
||||||
|
$from->WebSocket->frame->addBuffer($data);
|
||||||
|
if ($from->WebSocket->frame->isCoalesced()) {
|
||||||
|
if ($from->WebSocket->frame->getOpcode() > 2) {
|
||||||
|
$from->close();
|
||||||
|
throw new \UnexpectedValueException('Control frame support coming soon!');
|
||||||
|
}
|
||||||
|
// Check frame
|
||||||
|
// If is control frame, do your thing
|
||||||
|
// Else, add to message
|
||||||
|
// Control frames (ping, pong, close) can be sent in between a fragmented message
|
||||||
|
|
||||||
|
$from->WebSocket->message->addFrame($from->WebSocket->frame);
|
||||||
|
unset($from->WebSocket->frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($from->WebSocket->message->isCoalesced()) {
|
||||||
|
$parsed = (string)$from->WebSocket->message;
|
||||||
|
unset($from->WebSocket->message);
|
||||||
|
|
||||||
|
return $parsed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
39
WsServer.php
39
WsServer.php
@ -69,7 +69,7 @@ class WsServer implements MessageComponentInterface {
|
|||||||
// mb_internal_encoding('UTF-8');
|
// mb_internal_encoding('UTF-8');
|
||||||
|
|
||||||
$this->handshaker = new HandshakeNegotiator;
|
$this->handshaker = new HandshakeNegotiator;
|
||||||
//$this->messager = new MessageParser;
|
$this->messager = new MessageParser;
|
||||||
|
|
||||||
$this->_decorating = $component;
|
$this->_decorating = $component;
|
||||||
$this->connections = new \SplObjectStorage;
|
$this->connections = new \SplObjectStorage;
|
||||||
@ -115,41 +115,8 @@ class WsServer implements MessageComponentInterface {
|
|||||||
return $this->_decorating->onOpen($conn);
|
return $this->_decorating->onOpen($conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if (null !== ($parsed = $this->messager->onData($conn, $msg))) {
|
||||||
if (null !== ($parsed = $this->messager->onData($from, $msg))) {
|
$this->_decorating->onMessage($conn, $parsed);
|
||||||
$this->_decorating->onMessage($from, $parsed);
|
|
||||||
}
|
|
||||||
/**/
|
|
||||||
|
|
||||||
/*************************************************************/
|
|
||||||
|
|
||||||
if (!isset($from->WebSocket->message)) {
|
|
||||||
$from->WebSocket->message = $from->WebSocket->version->newMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
// There is a frame fragment attatched to the connection, add to it
|
|
||||||
if (!isset($from->WebSocket->frame)) {
|
|
||||||
$from->WebSocket->frame = $from->WebSocket->version->newFrame();
|
|
||||||
}
|
|
||||||
|
|
||||||
$from->WebSocket->frame->addBuffer($msg);
|
|
||||||
if ($from->WebSocket->frame->isCoalesced()) {
|
|
||||||
if ($from->WebSocket->frame->getOpcode() > 2) {
|
|
||||||
$from->close();
|
|
||||||
throw new \UnexpectedValueException('Control frame support coming soon!');
|
|
||||||
}
|
|
||||||
// Check frame
|
|
||||||
// If is control frame, do your thing
|
|
||||||
// Else, add to message
|
|
||||||
// Control frames (ping, pong, close) can be sent in between a fragmented message
|
|
||||||
|
|
||||||
$from->WebSocket->message->addFrame($from->WebSocket->frame);
|
|
||||||
unset($from->WebSocket->frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($from->WebSocket->message->isCoalesced()) {
|
|
||||||
$this->_decorating->onMessage($this->connections[$from], (string)$from->WebSocket->message);
|
|
||||||
unset($from->WebSocket->message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user