mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 23:55:35 +08:00
add websocket client functions
This commit is contained in:
@@ -2,11 +2,14 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Choir\Http\Client\Exception\ClientException;
|
||||
use Choir\Http\HttpFactory;
|
||||
use OneBot\Driver\Coroutine\Adaptive;
|
||||
use OneBot\Driver\Coroutine\CoroutineInterface;
|
||||
use OneBot\Driver\Interfaces\WebSocketClientInterface;
|
||||
use OneBot\Driver\Process\ExecutionResult;
|
||||
use OneBot\Driver\Socket\WSServerSocketBase;
|
||||
use OneBot\Driver\Workerman\WebSocketClient;
|
||||
use OneBot\V12\Object\MessageSegment;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
@@ -14,6 +17,7 @@ use Psr\SimpleCache\CacheInterface;
|
||||
use ZM\Config\Environment;
|
||||
use ZM\Config\ZMConfig;
|
||||
use ZM\Container\ContainerHolder;
|
||||
use ZM\Exception\DriverException;
|
||||
use ZM\Framework;
|
||||
use ZM\Logger\ConsoleLogger;
|
||||
use ZM\Middleware\MiddlewareHandler;
|
||||
@@ -363,3 +367,21 @@ function zm_create_plugin(): ZMPlugin
|
||||
{
|
||||
return new ZMPlugin();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个 WebSocket 客户端
|
||||
*
|
||||
* @param string $address 接入地址,例如 ws://192.168.1.3:9998/
|
||||
* @param array $header 请求头
|
||||
* @param null|mixed $set Swoole 驱动下传入的额外参数
|
||||
* @throws DriverException
|
||||
* @throws ClientException
|
||||
*/
|
||||
function zm_websocket_client(string $address, array $header = [], mixed $set = null): WebSocketClientInterface
|
||||
{
|
||||
return match (Framework::getInstance()->getDriver()->getName()) {
|
||||
'swoole' => \OneBot\Driver\Swoole\WebSocketClient::createFromAddress($address, $header, $set ?? ['websocket_mask' => true]),
|
||||
'workerman' => WebSocketClient::createFromAddress($address, $header),
|
||||
default => throw new DriverException('current driver is not supported for creating websocket client'),
|
||||
};
|
||||
}
|
||||
|
||||
9
src/ZM/Exception/DriverException.php
Normal file
9
src/ZM/Exception/DriverException.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Exception;
|
||||
|
||||
class DriverException extends ZMException
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user