Merge pull request #11 from limingxinleo/websocket

Added WebSocket Server.
This commit is contained in:
李铭昕 2019-06-21 16:58:59 +08:00 committed by GitHub
commit b6c1d248ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 260 additions and 3 deletions

View 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;
}

View 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;
}

View 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;
}

View File

@ -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.

View File

@ -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.
*/

View 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.

View 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"
}
}

View 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__,
],
],
];
}
}

View 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
{
}

View 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,
]);
}
}

View 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');
}
}