diff --git a/src/contract/src/OnCloseInterface.php b/src/contract/src/OnCloseInterface.php new file mode 100644 index 000000000..4b38f1a6a --- /dev/null +++ b/src/contract/src/OnCloseInterface.php @@ -0,0 +1,20 @@ +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. diff --git a/src/server/src/SwooleEvent.php b/src/server/src/SwooleEvent.php index 0330d10c2..bdc51be10 100644 --- a/src/server/src/SwooleEvent.php +++ b/src/server/src/SwooleEvent.php @@ -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. */ diff --git a/src/websocket-server/LICENSE.md b/src/websocket-server/LICENSE.md new file mode 100644 index 000000000..eb14702a3 --- /dev/null +++ b/src/websocket-server/LICENSE.md @@ -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. \ No newline at end of file diff --git a/src/websocket-server/composer.json b/src/websocket-server/composer.json new file mode 100644 index 000000000..e5953d6bb --- /dev/null +++ b/src/websocket-server/composer.json @@ -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" + } +} diff --git a/src/websocket-server/src/ConfigProvider.php b/src/websocket-server/src/ConfigProvider.php new file mode 100644 index 000000000..5bdf4f93a --- /dev/null +++ b/src/websocket-server/src/ConfigProvider.php @@ -0,0 +1,32 @@ + [ + Server::class => ServerFactory::class, + ], + 'commands' => [ + ], + 'scan' => [ + 'paths' => [ + __DIR__, + ], + ], + ]; + } +} diff --git a/src/websocket-server/src/CoreMiddleware.php b/src/websocket-server/src/CoreMiddleware.php new file mode 100644 index 000000000..8d11e588e --- /dev/null +++ b/src/websocket-server/src/CoreMiddleware.php @@ -0,0 +1,19 @@ +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, + ]); + } +} diff --git a/src/websocket-server/src/ServerFactory.php b/src/websocket-server/src/ServerFactory.php new file mode 100644 index 000000000..d1f2754df --- /dev/null +++ b/src/websocket-server/src/ServerFactory.php @@ -0,0 +1,23 @@ +