Skip to content

Commit

Permalink
update to 0.6.0, refactor server socket
Browse files Browse the repository at this point in the history
  • Loading branch information
crazywhalecc committed Mar 3, 2023
1 parent 6c6bc2d commit 3cb94c2
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 38 deletions.
8 changes: 5 additions & 3 deletions src/OneBot/Driver/Interfaces/WebSocketInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
interface WebSocketInterface
{
/**
* @param null|int|string $id 连接ID
* @param FrameInterface $data 数据
* WebSocket 发送一条数据帧,可传入 Frame 对象,也可直接传入字符串
*
* @param null|int|string $fd 连接ID
* @param FrameInterface|string $data 数据
*/
public function send(FrameInterface $data, $id = null): bool;
public function send($data, $fd): bool;
}
10 changes: 10 additions & 0 deletions src/OneBot/Driver/Socket/SocketTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ trait SocketTrait

/* ======================== Getter by flags ======================== */

public function getWSServerSocketByFlag(int $flag = 0): ?WSServerSocketBase
{
foreach ($this->ws_socket as $socket) {
if ($socket->getFlag() === $flag) {
return $socket;
}
}
return null;
}

/**
* @return \Generator|WSServerSocketBase[]
*/
Expand Down
3 changes: 1 addition & 2 deletions src/OneBot/Driver/Socket/WSClientSocketBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace OneBot\Driver\Socket;

use Choir\WebSocket\FrameInterface;
use OneBot\Driver\Interfaces\SocketInterface;
use OneBot\Driver\Interfaces\WebSocketClientInterface;
use OneBot\Driver\Interfaces\WebSocketInterface;
Expand Down Expand Up @@ -63,7 +62,7 @@ public function getClient(): WebSocketClientInterface
return $this->client;
}

public function send(FrameInterface $data, $id = null): bool
public function send($data, $fd = null): bool
{
return $this->client->send($data);
}
Expand Down
5 changes: 2 additions & 3 deletions src/OneBot/Driver/Socket/WSServerSocketBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace OneBot\Driver\Socket;

use Choir\WebSocket\FrameInterface;
use OneBot\Driver\Interfaces\SocketInterface;
use OneBot\Driver\Interfaces\WebSocketInterface;

Expand All @@ -13,7 +12,7 @@ abstract class WSServerSocketBase implements SocketInterface, WebSocketInterface
use SocketFlag;
use SocketConfig;

abstract public function sendAll(FrameInterface $data): array;
abstract public function sendMultiple($data, ?callable $filter = null): array;

abstract public function close($id): bool;
abstract public function close($fd): bool;
}
43 changes: 30 additions & 13 deletions src/OneBot/Driver/Swoole/Socket/WSServerSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,51 @@

use Choir\WebSocket\FrameInterface;
use OneBot\Driver\Socket\WSServerSocketBase;
use Swoole\Server;
use Swoole\Server\Port;
use Swoole\WebSocket\Server;

class WSServerSocket extends WSServerSocketBase
{
/** @var Server|Server\Port|\Swoole\Http\Server|\Swoole\WebSocket\Server */
protected $socket_obj;
protected ?Server $server;

public function __construct($server_or_port, array $config)
{
$this->socket_obj = $server_or_port;
$this->config = $config;
}
protected ?Port $port;

public function sendAll(FrameInterface $data): array
public function __construct(?Server $server = null, ?Port $port = null, array $config = [])
{
return [];
$this->server = $server;
$this->port = $port;
$this->config = $config;
}

public function close($id): bool
public function close($fd): bool
{
return false;
}

/**
* {@inheritDoc}
*/
public function send(FrameInterface $data, $id = null): bool
public function send($data, $fd): bool
{
return false;
if ($data instanceof FrameInterface) {
return $this->server->push($fd, $data->getData(), $data->getOpcode());
}
return $this->server->push($fd, $data);
}

public function sendMultiple($data, ?callable $filter = null): array
{
$result = [];
if ($this->port !== null) {
$a = $this->port->connections;
} else {
$a = $this->server->connections;
}
foreach ($a as $fd) {
if ($this->server->exists($fd) && ($filter === null || $filter($fd, $this))) {
$result[$fd] = $this->send($data, $fd);
}
}
return $result;
}
}
4 changes: 2 additions & 2 deletions src/OneBot/Driver/Swoole/SwooleDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function initInternalDriverClasses(?array $http, ?array $http_webhook, ?a
$this->server = new SwooleWebSocketServer($ws_0['host'], $ws_0['port'], $this->getParam('swoole_server_mode', SWOOLE_PROCESS));
$this->initServer();
$this->initWebSocketServer($this->server, $ws_0);
$this->ws_socket[] = new WSServerSocket($this->server, $ws_0);
$this->ws_socket[] = new WSServerSocket($this->server, null, $ws_0);
if (!empty($ws)) {
foreach ($ws as $v) {
$this->addWSServerListener($v);
Expand Down Expand Up @@ -261,7 +261,7 @@ private function addWSServerListener($v)
'open_http_protocol' => false,
]);
$this->initWebSocketServer($port, $v);
$this->ws_socket[] = new WSServerSocket($port, $v);
$this->ws_socket[] = new WSServerSocket($this->server, $port, $v);
}

private function addHttpServerListener($v)
Expand Down
45 changes: 31 additions & 14 deletions src/OneBot/Driver/Workerman/Socket/WSServerSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,64 @@

class WSServerSocket extends WSServerSocketBase
{
/**
* @var Worker
*/
public $worker;
public Worker $worker;

/**
* @var TcpConnection[]
*/
public $connections = [];
public array $connections = [];

public function __construct(Worker $worker)
{
$this->worker = $worker;
}

public function send(FrameInterface $data, $id = null): bool
public function send($data, $fd): bool
{
if (!isset($this->connections[$id])) {
if (!isset($this->connections[$fd])) {
ob_logger()->warning('链接不存在,可能已被关闭或未连接');
return false;
}
return $this->connections[$id]->send($data->getData());
if ($data instanceof FrameInterface) {
$data = $data->getData();
}
return $this->connections[$fd]->send($data->getData());
}

public function sendAll(FrameInterface $data): array
public function sendMultiple($data, ?callable $filter = null): array
{
$result = [];
if ($data instanceof FrameInterface) {
$data = $data->getData();
}
foreach ($this->connections as $fd => $connection) {
if ($connection->getStatus() === TcpConnection::STATUS_ESTABLISHED && ($filter === null || $filter($fd, $this))) {
$result[$fd] = $connection->send($data->getData());
}
}
return $result;
}

public function sendAll($data): array
{
$result = [];
if ($data instanceof FrameInterface) {
$data = $data->getData();
}
foreach ($this->connections as $id => $connection) {
$result[$id] = $connection->send($data->getData());
$result[$id] = $connection->send($data);
}
return $result;
}

public function close($id): bool
public function close($fd): bool
{
if (!isset($this->connections[$id])) {
if (!isset($this->connections[$fd])) {
ob_logger()->warning('链接不存在,可能已被关闭或未连接');
return false;
}
$this->connections[$id]->close();
unset($this->connections[$id]);
$this->connections[$fd]->close();
unset($this->connections[$fd]);
return true;
}

Expand Down
6 changes: 6 additions & 0 deletions src/OneBot/Driver/Workerman/WebSocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Choir\Http\HttpFactory;
use Choir\WebSocket\FrameFactory;
use Choir\WebSocket\FrameInterface;
use OneBot\Driver\Interfaces\WebSocketClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
Expand Down Expand Up @@ -114,6 +115,11 @@ public function setCloseCallback($callable): WebSocketClientInterface

public function send($data): bool
{
if ($data instanceof FrameInterface) {
$data = $data->getData();
} elseif (!is_string($data)) {
return false;
}
$this->connection->send($data);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/OneBot/global_defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use ZM\Logger\ConsoleLogger;

const ONEBOT_VERSION = '12';
const ONEBOT_LIBOB_VERSION = '0.5.11';
const ONEBOT_LIBOB_VERSION = '0.6.0';

const ONEBOT_JSON = 1;
const ONEBOT_MSGPACK = 2;
Expand Down

0 comments on commit 3cb94c2

Please sign in to comment.