2020-09-02 11:38:47 +08:00
|
|
|
<?php
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-09-02 11:38:47 +08:00
|
|
|
|
|
|
|
|
namespace HttpServer\Events;
|
|
|
|
|
|
|
|
|
|
|
2020-12-15 14:04:02 +08:00
|
|
|
use Annotation\Route\Socket;
|
2020-11-20 14:38:20 +08:00
|
|
|
use Exception;
|
2020-09-04 01:05:33 +08:00
|
|
|
use HttpServer\Abstracts\Callback;
|
2021-02-20 16:15:01 +08:00
|
|
|
use HttpServer\Http\Context;
|
2021-03-03 14:10:53 +08:00
|
|
|
use HttpServer\Http\HttpHeaders;
|
|
|
|
|
use HttpServer\Http\HttpParams;
|
|
|
|
|
use HttpServer\Http\Request;
|
|
|
|
|
use ReflectionException;
|
2021-02-20 16:23:23 +08:00
|
|
|
use Snowflake\Abstracts\Config;
|
2021-02-24 14:32:10 +08:00
|
|
|
use Snowflake\Core\Json;
|
2020-09-02 11:38:47 +08:00
|
|
|
use Snowflake\Event;
|
2021-01-04 17:43:01 +08:00
|
|
|
use Snowflake\Exception\ComponentException;
|
2021-03-03 14:10:53 +08:00
|
|
|
use Snowflake\Exception\ConfigException;
|
|
|
|
|
use Snowflake\Exception\NotFindClassException;
|
2020-09-02 11:38:47 +08:00
|
|
|
use Snowflake\Snowflake;
|
2020-11-20 14:15:37 +08:00
|
|
|
use Swoole\Coroutine;
|
2020-09-02 11:38:47 +08:00
|
|
|
use Swoole\WebSocket\Frame;
|
|
|
|
|
use Swoole\WebSocket\Server;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class OnMessage
|
|
|
|
|
* @package HttpServer\Events
|
|
|
|
|
*/
|
|
|
|
|
class OnMessage extends Callback
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param Frame $frame
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
public function onHandler(Server $server, Frame $frame)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2021-03-31 15:47:32 +08:00
|
|
|
if ($frame->opcode === 0x08) {
|
2021-02-24 14:32:10 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2021-03-31 14:53:19 +08:00
|
|
|
$clientInfo = $server->getClientInfo($frame->fd);
|
2021-02-24 14:32:10 +08:00
|
|
|
$event = Snowflake::app()->getEvent();
|
2021-03-31 15:46:20 +08:00
|
|
|
|
2021-03-31 15:46:33 +08:00
|
|
|
if (!$event->exists(($name = $this->getName($clientInfo)))) {
|
2021-03-31 14:53:19 +08:00
|
|
|
return;
|
2020-09-02 11:38:47 +08:00
|
|
|
}
|
2021-03-31 14:53:19 +08:00
|
|
|
$event->trigger($name, [$frame, $server]);
|
2020-11-06 16:47:17 +08:00
|
|
|
} catch (\Throwable $exception) {
|
2021-02-20 15:45:48 +08:00
|
|
|
$this->addError($exception, 'websocket');
|
2020-09-02 11:38:47 +08:00
|
|
|
$server->send($frame->fd, $exception->getMessage());
|
2021-03-03 14:18:03 +08:00
|
|
|
} finally {
|
|
|
|
|
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
|
|
|
|
logger()->insert();
|
2020-09-02 11:38:47 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-03 14:10:53 +08:00
|
|
|
|
|
|
|
|
/**
|
2021-03-31 14:53:19 +08:00
|
|
|
* @param $clientInfo
|
|
|
|
|
* @return string
|
2021-03-03 14:10:53 +08:00
|
|
|
*/
|
2021-03-31 14:53:19 +08:00
|
|
|
private function getName($clientInfo): string
|
2021-03-03 14:10:53 +08:00
|
|
|
{
|
2021-03-31 14:53:19 +08:00
|
|
|
return 'listen ' . $clientInfo['server_port'] . ' ' . Event::SERVER_MESSAGE;
|
2020-11-20 14:38:20 +08:00
|
|
|
}
|
|
|
|
|
|
2020-09-02 11:38:47 +08:00
|
|
|
}
|