This commit is contained in:
2020-08-31 14:52:28 +08:00
parent df49c40163
commit 8b1bf6c04a
6 changed files with 24 additions and 8 deletions
+5 -1
View File
@@ -18,6 +18,7 @@ use Swoole\Http\Request;
use Swoole\Http\Response; use Swoole\Http\Response;
use Exception; use Exception;
use Swoole\Http\Server; use Swoole\Http\Server;
use Swoole\Process\Pool;
class Http extends Server class Http extends Server
{ {
@@ -43,15 +44,18 @@ class Http extends Server
/** /**
* @param array $settings * @param array $settings
* @param null $pool
* @param array $events * @param array $events
* @param array $config * @param array $config
* @return mixed|void * @return mixed|void
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception
*/ */
public function set(array $settings, $events = [], $config = []) public function set(array $settings, $pool = null, $events = [], $config = [])
{ {
parent::set($settings); parent::set($settings);
Snowflake::get()->set(Pool::class, $pool);
ServerManager::set($this, $settings, $this->application, $events, $config); ServerManager::set($this, $settings, $this->application, $events, $config);
} }
+4 -1
View File
@@ -52,15 +52,18 @@ abstract class Service extends Server
/** /**
* @param array $settings * @param array $settings
* @param null $pool
* @param array $events * @param array $events
* @param array $config * @param array $config
* @return mixed|void * @return mixed|void
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception
*/ */
public function set(array $settings, $events = [], $config = []) public function set(array $settings, $pool = null, $events = [], $config = [])
{ {
parent::set($settings); parent::set($settings);
Snowflake::get()->set(Pool::class, $pool);
ServerManager::set($this, $settings, $this->application, $events, $config); ServerManager::set($this, $settings, $this->application, $events, $config);
} }
+9 -3
View File
@@ -10,12 +10,14 @@ namespace HttpServer\Events;
use Exception; use Exception;
use HttpServer\ServerManager; use HttpServer\ServerManager;
use ReflectionException;
use Snowflake\Error\Logger; use Snowflake\Error\Logger;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Http\Request as SRequest; use Swoole\Http\Request as SRequest;
use Swoole\Http\Response as SResponse; use Swoole\Http\Response as SResponse;
use Swoole\Process\Pool;
use Swoole\WebSocket\Frame; use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server; use Swoole\WebSocket\Server;
@@ -50,15 +52,19 @@ class WebSocket extends Server
/** /**
* @param array $settings * @param array $settings
* @param null $pool
* @param array $events * @param array $events
* @param $config * @param array $config
* @return mixed|void * @return mixed|void
* @throws \ReflectionException
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/ */
public function set(array $settings, $events = [], $config = []) public function set(array $settings, $pool = null, $events = [], $config = [])
{ {
parent::set($settings); parent::set($settings);
Snowflake::get()->set(WebSocket::class, $this);
Snowflake::get()->set(Pool::class, $pool);
ServerManager::set($this, $settings, $this->application, $events, $config); ServerManager::set($this, $settings, $this->application, $events, $config);
} }
+1 -1
View File
@@ -26,7 +26,7 @@ class ServerManager
} }
[$category, $config, $handlers, $settings] = $process; [$category, $config, $handlers, $settings] = $process;
$server = new $category[1](...static::parameter($application, $config, $category)); $server = new $category[1](...static::parameter($application, $config, $category));
$server->set($settings ?? [], $handlers, $config); $server->set($settings ?? [], $pool, $handlers, $config);
static::notice($application, $workerId, $config); static::notice($application, $workerId, $config);
if (property_exists($server, 'pack')) { if (property_exists($server, 'pack')) {
$server->pack = $config['message']['pack'] ?? function ($data) { $server->pack = $config['message']['pack'] ?? function ($data) {
+4 -1
View File
@@ -57,7 +57,6 @@ return [
], ],
'message' => [ 'message' => [
'pack' => function ($data) { 'pack' => function ($data) {
var_dump($data);
return \Snowflake\Core\JSON::encode($data); return \Snowflake\Core\JSON::encode($data);
}, },
'unpack' => function ($data) { 'unpack' => function ($data) {
@@ -86,6 +85,9 @@ return [
'receive' => function ($server, int $fd, int $reactorId, string $data) { 'receive' => function ($server, int $fd, int $reactorId, string $data) {
$server->push(1, 'success.'); $server->push(1, 'success.');
$server->send($fd, 'success.'); $server->send($fd, 'success.');
$socket = Snowflake::get()->get(\HttpServer\Events\WebSocket::class);
$socket->push(1, 'hello word~~~~~~~~~~~~~');
}, },
'settings' => [] 'settings' => []
], ],
@@ -103,6 +105,7 @@ return [
}, },
Event::SERVER_MESSAGE => function (\Swoole\WebSocket\Server $server, Frame $frame) { Event::SERVER_MESSAGE => function (\Swoole\WebSocket\Server $server, Frame $frame) {
$this->error('websocket SERVER_MESSAGE.'); $this->error('websocket SERVER_MESSAGE.');
return $server->push($frame->fd, 'hello word~'); return $server->push($frame->fd, 'hello word~');
}, },
Event::SERVER_CLOSE => function (int $fd) { Event::SERVER_CLOSE => function (int $fd) {
+1 -1
View File
@@ -43,7 +43,7 @@
} }
tick = setInterval(function () { tick = setInterval(function () {
sock.send('tick'); sock.send('tick');
}, 30000) }, 3000)
} }
} }