Skip to content

Commit

Permalink
兼容php7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Jan 21, 2021
1 parent 474c38d commit 1113099
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/websocket/socketio/EnginePacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function message($payload)

public static function fromString(string $packet)
{
return new static($packet{0}, substr($packet, 1) ?? '');
return new static(substr($packet, 0, 1), substr($packet, 1) ?? '');
}

public function toString()
Expand Down
8 changes: 4 additions & 4 deletions src/websocket/socketio/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ public function onMessage(Frame $frame)
$this->onConnect($packet->data);
break;
case Packet::EVENT:
case Packet::ACK:
$result = $this->event->trigger('swoole.websocket.Event', $packet->data);
[$type, $data] = $packet->data;
$result = $this->event->trigger('swoole.websocket.Event', ['type' => $type, 'data' => $data]);

if ($packet->id !== null) {
$responsePacket = Packet::create(Packet::ACK, [
'id' => $packet->id,
'nsp' => $packet->nsp,
'data' => end($result),
'data' => $result,
]);

$this->push($responsePacket);
Expand Down Expand Up @@ -160,7 +160,7 @@ protected function resetPingTimeout($timeout)
protected function schedulePing()
{
Timer::clear($this->pingIntervalTimer);
$this->pingIntervalTimer = Timer::after($this->pingIntervalTimer, function () {
$this->pingIntervalTimer = Timer::after($this->pingInterval, function () {
$this->push(EnginePacket::ping());
$this->resetPingTimeout($this->pingTimeout);
});
Expand Down
16 changes: 8 additions & 8 deletions src/websocket/socketio/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static function encode(Packet $packet)
$str .= $packet->nsp . ',';
}

if (!empty($packet->id)) {
if ($packet->id !== null) {
$str .= $packet->id;
}

Expand All @@ -26,13 +26,13 @@ public static function decode(string $str)
{
$i = 0;

$packet = new Packet((int) $str{0});
$packet = new Packet((int) substr($str, 0, 1));

// look up namespace (if any)
if ('/' === $str{$i + 1}) {
if ('/' === substr($str, $i + 1, 1)) {
$nsp = '';
while (++$i) {
$c = $str{$i};
$c = substr($str, $i, 1);
if (',' === $c) {
break;
}
Expand All @@ -47,16 +47,16 @@ public static function decode(string $str)
}

// look up id
$next = $str{$i + 1};
$next = substr($str, $i + 1, 1);
if ('' !== $next && is_numeric($next)) {
$id = '';
while (++$i) {
$c = $str{$i};
$c = substr($str, $i, 1);
if (null == $c || !is_numeric($c)) {
--$i;
break;
}
$id .= $str{$i};
$id .= substr($str, $i, 1);
if ($i === strlen($str)) {
break;
}
Expand All @@ -65,7 +65,7 @@ public static function decode(string $str)
}

// look up json data
if ($str{++$i}) {
if (substr($str, ++$i, 1)) {
$packet->data = json_decode(substr($str, $i), true);
}

Expand Down

0 comments on commit 1113099

Please sign in to comment.