Skip to content

Commit

Permalink
fix: ws->push() compatible old and latest version (#539)
Browse files Browse the repository at this point in the history
fix: ws->push() compatible old and latest version
  • Loading branch information
inhere authored Nov 18, 2019
2 parents 16557ce + 43a5075 commit e57c804
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/websocket-server/src/Helper/WsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Swoft\WebSocket\Server\Helper;

use Swoft\Console\Console;
use Swoole\Http\Request;
use Swoole\Http\Response;
use function base64_decode;
Expand All @@ -10,6 +11,7 @@
use function sha1;
use function strlen;
use function trim;
use const SWOOLE_VERSION;

/**
* Class WsHelper
Expand Down Expand Up @@ -105,4 +107,21 @@ public static function fastHandshake(Request $request, Response $response): bool
$response->end();
return true;
}

/**
* @param string $version
*
* @return bool
*/
public static function isLtSwooleVersion(string $version = '4.4.12'): bool
{
$curVer = SWOOLE_VERSION;

if (version_compare($curVer, $version, '<')) {
Console::colored("[NOTICE] Swoole current version is {$curVer}, suggestion upgrade to {$version}+", 'warning');
return true;
}

return false;
}
}
6 changes: 6 additions & 0 deletions src/websocket-server/src/WebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Swoft\Server\Exception\ServerException;
use Swoft\Server\Server;
use Swoft\Server\SwooleEvent;
use Swoft\WebSocket\Server\Helper\WsHelper;
use Swoole\Websocket\Frame;
use Throwable;
use function array_flip;
Expand Down Expand Up @@ -125,6 +126,11 @@ public function sendTo(
$fromUser = $sender < 1 ? 'SYSTEM' : $sender;
$this->log("(private)The #{$fromUser} send message to the user #{$receiver}. Opcode: $opcode Data: {$data}");

// Fix: since swoole 4.4.12 $finish change type to int.
if (WsHelper::isLtSwooleVersion()) {
$finish = (bool)$finish;
}

return $this->swooleServer->push($receiver, $data, $opcode, $finish);
}

Expand Down

0 comments on commit e57c804

Please sign in to comment.