mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 03:37:44 +08:00
Merge pull request #11 from limingxinleo/websocket
Added WebSocket Server.
This commit is contained in:
commit
b6c1d248ab
20
src/contract/src/OnCloseInterface.php
Normal file
20
src/contract/src/OnCloseInterface.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace Hyperf\Contract;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
interface OnCloseInterface
|
||||
{
|
||||
public function onClose(Server $server, int $fd, int $reactorId): void;
|
||||
}
|
21
src/contract/src/OnMessageInterface.php
Normal file
21
src/contract/src/OnMessageInterface.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace Hyperf\Contract;
|
||||
|
||||
use Swoole\Server;
|
||||
use Swoole\Websocket\Frame;
|
||||
|
||||
interface OnMessageInterface
|
||||
{
|
||||
public function onMessage(Server $server, Frame $frame): void;
|
||||
}
|
21
src/contract/src/OnOpenInterface.php
Normal file
21
src/contract/src/OnOpenInterface.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace Hyperf\Contract;
|
||||
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Server;
|
||||
|
||||
interface OnOpenInterface
|
||||
{
|
||||
public function onOpen(Server $server, Request $request): void;
|
||||
}
|
@ -181,11 +181,11 @@ class Server implements ServerInterface
|
||||
}
|
||||
if (is_array($callback)) {
|
||||
[$className, $method] = $callback;
|
||||
if (array_key_exists($className, $this->onRequestCallbacks)) {
|
||||
$this->logger->warning(sprintf('%s will be replaced by %s, each server should has own onRequest callback, please check your configs.', $this->onRequestCallbacks[$callback[0]], $serverName));
|
||||
if (array_key_exists($className . $method, $this->onRequestCallbacks)) {
|
||||
$this->logger->warning(sprintf('%s will be replaced by %s, each server should has own onRequest callback, please check your configs.', $this->onRequestCallbacks[$className . $method], $serverName));
|
||||
}
|
||||
|
||||
$this->onRequestCallbacks[$className] = $serverName;
|
||||
$this->onRequestCallbacks[$className . $method] = $serverName;
|
||||
$class = $this->container->get($className);
|
||||
if (method_exists($class, 'setServerName')) {
|
||||
// Override the server name.
|
||||
|
@ -54,6 +54,16 @@ class SwooleEvent
|
||||
*/
|
||||
const ON_CONNECT = 'connect';
|
||||
|
||||
/**
|
||||
* Swoole onOpen event.
|
||||
*/
|
||||
const ON_OPEN = 'open';
|
||||
|
||||
/**
|
||||
* Swoole onMessage event.
|
||||
*/
|
||||
const ON_MESSAGE = 'message';
|
||||
|
||||
/**
|
||||
* Swoole onClose event.
|
||||
*/
|
||||
|
9
src/websocket-server/LICENSE.md
Normal file
9
src/websocket-server/LICENSE.md
Normal file
@ -0,0 +1,9 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Hyperf
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
59
src/websocket-server/composer.json
Normal file
59
src/websocket-server/composer.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"name": "hyperf/websocket-server",
|
||||
"description": "A websocket server library for Hyperf.",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"php",
|
||||
"swoole",
|
||||
"hyperf",
|
||||
"websocket"
|
||||
],
|
||||
"support": {
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.2",
|
||||
"ext-swoole": ">=4.3",
|
||||
"hyperf/contract": "~1.0.0",
|
||||
"hyperf/http-server": "~1.0.0",
|
||||
"hyperf/utils": "~1.0.0",
|
||||
"psr/container": "^1.0",
|
||||
"psr/event-dispatcher": "^1.0",
|
||||
"psr/log": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^2.9",
|
||||
"malukenho/docheader": "^0.1.6",
|
||||
"mockery/mockery": "^1.0",
|
||||
"phpunit/phpunit": "^7.0"
|
||||
},
|
||||
"suggest": {
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
],
|
||||
"psr-4": {
|
||||
"Hyperf\\WebSocketServer\\": "src"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.0-dev"
|
||||
},
|
||||
"hyperf": {
|
||||
"config": "Hyperf\\WebSocketServer\\ConfigProvider"
|
||||
}
|
||||
},
|
||||
"bin": [
|
||||
],
|
||||
"scripts": {
|
||||
"cs-fix": "php-cs-fixer fix $1",
|
||||
"test": "phpunit --colors=always"
|
||||
}
|
||||
}
|
32
src/websocket-server/src/ConfigProvider.php
Normal file
32
src/websocket-server/src/ConfigProvider.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace Hyperf\WebSocketServer;
|
||||
|
||||
class ConfigProvider
|
||||
{
|
||||
public function __invoke(): array
|
||||
{
|
||||
return [
|
||||
'dependencies' => [
|
||||
Server::class => ServerFactory::class,
|
||||
],
|
||||
'commands' => [
|
||||
],
|
||||
'scan' => [
|
||||
'paths' => [
|
||||
__DIR__,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
19
src/websocket-server/src/CoreMiddleware.php
Normal file
19
src/websocket-server/src/CoreMiddleware.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace Hyperf\WebSocketServer;
|
||||
|
||||
use Hyperf\HttpServer\CoreMiddleware as HttpCoreMiddleware;
|
||||
|
||||
class CoreMiddleware extends HttpCoreMiddleware
|
||||
{
|
||||
}
|
43
src/websocket-server/src/Server.php
Normal file
43
src/websocket-server/src/Server.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace Hyperf\WebSocketServer;
|
||||
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Contract\MiddlewareInitializerInterface;
|
||||
use Hyperf\Contract\OnRequestInterface;
|
||||
use Hyperf\Dispatcher\HttpDispatcher;
|
||||
use Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler;
|
||||
use Hyperf\HttpServer\Server as HttpServer;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class Server extends HttpServer implements MiddlewareInitializerInterface, OnRequestInterface
|
||||
{
|
||||
public function __construct(ContainerInterface $container, string $serverName)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->serverName = $serverName;
|
||||
$this->dispatcher = $container->get(HttpDispatcher::class);
|
||||
}
|
||||
|
||||
public function initCoreMiddleware(string $serverName): void
|
||||
{
|
||||
$this->serverName = $serverName;
|
||||
$this->coreMiddleware = new CoreMiddleware($this->container, $serverName);
|
||||
|
||||
$config = $this->container->get(ConfigInterface::class);
|
||||
$this->middlewares = $config->get('middlewares.' . $serverName, []);
|
||||
$this->exceptionHandlers = $config->get('exceptions.handler.' . $serverName, [
|
||||
HttpExceptionHandler::class,
|
||||
]);
|
||||
}
|
||||
}
|
23
src/websocket-server/src/ServerFactory.php
Normal file
23
src/websocket-server/src/ServerFactory.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace Hyperf\WebSocketServer;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class ServerFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container): Server
|
||||
{
|
||||
return new Server($container, 'websocket');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user