diff --git a/src/websocket-server/src/Helper/WsHelper.php b/src/websocket-server/src/Helper/WsHelper.php index 715ebbbba..56777605d 100644 --- a/src/websocket-server/src/Helper/WsHelper.php +++ b/src/websocket-server/src/Helper/WsHelper.php @@ -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; @@ -10,6 +11,7 @@ use function sha1; use function strlen; use function trim; +use const SWOOLE_VERSION; /** * Class WsHelper @@ -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; + } } diff --git a/src/websocket-server/src/WebSocketServer.php b/src/websocket-server/src/WebSocketServer.php index 25b6ba4b4..594a19f2b 100644 --- a/src/websocket-server/src/WebSocketServer.php +++ b/src/websocket-server/src/WebSocketServer.php @@ -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; @@ -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); }