Skip to content

Commit

Permalink
Log ignored packet when wait for event.
Browse files Browse the repository at this point in the history
Signed-off-by: Toha <tohenk@yahoo.com>
  • Loading branch information
tohenk committed Feb 4, 2024
1 parent 3190097 commit c876ef0
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
38 changes: 36 additions & 2 deletions src/Engine/AbstractSocketIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,15 @@ public function emit($event, array $args)
public function wait($event)
{
while (true) {
if (($packet = $this->drain()) && $found = $this->matchEvent($packet, $event)) {
return $found;
if ($packet = $this->drain()) {
if ($found = $this->matchEvent($packet, $event)) {
return $found;
}
foreach ($this->flattenPacket($packet) as $p) {
if ($info = $this->getPacketInfo($p)) {
$this->logger->debug(sprintf('Ignoring packet: %s', Util::truncate($this->stringifyPacket($info))));
}
}
}
}
}
Expand Down Expand Up @@ -539,6 +546,33 @@ protected function peekPacket($packet, $proto)
}
}

/**
* Get packet info.
*
* @param \stdClass $packet
* @return array
*/
protected function getPacketInfo($packet)
{
}

/**
* Convert packet data to string.
*
* @param array $packet
* @return string
*/
protected function stringifyPacket($packet)
{
$proto = $packet['proto'];
unset($packet['proto']);
array_walk($packet, function(&$value, $key) {
$value = sprintf('%s:%s', $key, is_array($value) ? json_encode($value) : var_export($value, true));
});

return sprintf('%s{%s}', strtoupper($proto), implode(', ', $packet));
}

/**
* Store successful connection handshake as session.
*
Expand Down
28 changes: 28 additions & 0 deletions src/Engine/SocketIO/Version0X.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,33 @@ protected function matchEvent($packet, $event)
}
}

/** {@inheritDoc} */
protected function getPacketInfo($packet)
{
$protocols = [
static::PROTO_DISCONNECT => 'disconnect',
static::PROTO_CONNECT => 'connect',
static::PROTO_HEARTBEAT => 'heartbeat',
static::PROTO_MESSAGE => 'message',
static::PROTO_JSON => 'json',
static::PROTO_EVENT => 'event',
static::PROTO_ACK => 'ack',
static::PROTO_ERROR => 'error',
static::PROTO_NOOP => 'noop',
];
$info = ['proto' => $protocols[$packet->proto]];
foreach (['nsp', 'event', 'args', 'data'] as $prop) {
if (isset($packet->$prop)) {
$info[$prop] = $packet->$prop;
if ($prop === 'args') {
break;
}
}
}

return $info;
}

/**
* Create payload.
*
Expand Down Expand Up @@ -188,6 +215,7 @@ protected function decodePacket($data)
}
break;
}
$this->logger->debug(sprintf('Got packet: %s', Util::truncate($this->stringifyPacket($this->getPacketInfo($packet)))));

return $packet;
}
Expand Down
40 changes: 39 additions & 1 deletion src/Engine/SocketIO/Version1X.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,44 @@ protected function matchEvent($packet, $event)
}
}

/** {@inheritDoc} */
protected function getPacketInfo($packet)
{
$protocols = [
static::PROTO_CLOSE => 'close',
static::PROTO_OPEN => 'open',
static::PROTO_PING => 'ping',
static::PROTO_PONG => 'pong',
static::PROTO_MESSAGE => 'message',
static::PROTO_UPGRADE => 'upgrade',
static::PROTO_NOOP => 'noop',
];
$packets = [
static::PACKET_CONNECT => 'connect',
static::PACKET_DISCONNECT => 'disconnect',
static::PACKET_EVENT => 'event',
static::PACKET_ACK => 'ack',
static::PACKET_ERROR => 'error',
static::PACKET_BINARY_EVENT => 'binary-event',
static::PACKET_BINARY_ACK => 'binary-ack',
];
$info = ['proto' => $protocols[$packet->proto]];
foreach (['type', 'nsp', 'event', 'args', 'data'] as $prop) {
if (isset($packet->$prop)) {
if ('type' === $prop) {
$info[$prop] = $packets[$packet->$prop];
} else {
$info[$prop] = $packet->$prop;
if ($prop === 'args') {
break;
}
}
}
}

return $info;
}

/**
* Create payload.
*
Expand Down Expand Up @@ -293,7 +331,6 @@ protected function decodePacket($data)
$packet->type = (int) $seq->read();
if ($packet->type === static::PACKET_BINARY_EVENT) {
$packet->binCount = (int) $seq->readUntil('-');
$seq->read();
}
$packet->nsp = $seq->readUntil(',[{', ['[', '{']);
if (null !== ($data = json_decode($seq->getData(), true))) {
Expand All @@ -316,6 +353,7 @@ protected function decodePacket($data)
}
break;
}
$this->logger->debug(sprintf('Got packet: %s', Util::truncate($this->stringifyPacket($this->getPacketInfo($packet)))));

return $packet;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Stream/SocketStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function request($uri, $headers = [], $options = [])
break;
}
if ($content = ($header || $len === null) ? fgets($this->handle) : fread($this->handle, (int) $len)) {
$this->logger->debug(sprintf('Receive: %s', trim($content)));
$this->logger->debug(sprintf('Receive: %s', Util::truncate(trim($content))));
if ($content === static::EOL && $header && count($this->result['headers'])) {
if ($skip_body) {
break;
Expand Down

0 comments on commit c876ef0

Please sign in to comment.