Skip to content

Commit

Permalink
update some classes
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 2, 2019
1 parent e198564 commit 5ea10ec
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/server/src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public function onWorkerError(CoServer $server, int $workerId, int $workerPid, i

$event = new WorkerEvent(SwooleEvent::WORKER_ERROR, $server, $workerId);

// is task process
// It's task process
$event->taskProcess = $workerId >= $server->setting['worker_num'];
$event->setParams([
'signal' => $signal,
Expand Down Expand Up @@ -1200,6 +1200,7 @@ public function getProcess(): array
*/
protected function defaultSetting(): array
{
/** @noinspection PhpVoidFunctionResultUsedInspection */
return [
'daemonize' => 0,
'worker_num' => swoole_cpu_num(),
Expand Down
53 changes: 53 additions & 0 deletions src/websocket-server/src/Listener/WorkerEndSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);

namespace Swoft\WebSocket\Server\Listener;

use Swoft\Event\Annotation\Mapping\Subscriber;
use Swoft\Event\EventInterface;
use Swoft\Event\EventSubscriberInterface;
use Swoft\Server\SwooleEvent;
use Swoft\Session\Session;
use Swoft\WebSocket\Server\WebSocketServer;

/**
* Class WorkerEndSubscriber
*
* @since 2.0.5
* TODO Subscriber()
*/
class WorkerEndSubscriber implements EventSubscriberInterface
{
/**
* Configure events and corresponding processing methods (you can configure the priority)
*
* @return array
* [
* 'event name' => 'handler method'
* 'event name' => ['handler method', priority]
* ]
*/
public static function getSubscribedEvents(): array
{
return [
SwooleEvent::WORKER_STOP => 'onWorkerStop',
];
}

/**
* @param EventInterface $event
*/
public function onWorkerStop(EventInterface $event): void
{
/** @var WebSocketServer $server */
$server = $event->getTarget();

// Close all connection
if ($server instanceof WebSocketServer) {
foreach (Session::getSessions() as $sid => $sess) {
$server->getSwooleServer()->disconnect((int)$sid, 0, 'closed by server');
}
}

Session::clear();
}
}
2 changes: 1 addition & 1 deletion src/websocket-server/src/WsMessageDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* @Bean("wsMsgDispatcher")
*/
class WsMessageDispatcher
class WsMessageDispatcher // extends \Swoft\Concern\AbstractDispatcher
{
/**
* Dispatch ws message handle
Expand Down
33 changes: 33 additions & 0 deletions src/websocket-server/test/unit/WsMessageDispatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace SwoftTest\WebSocket\Server\Unit;

use PHPUnit\Framework\TestCase;

/**
* Class WsMessageDispatcherTest
*/
class WsMessageDispatcherTest extends TestCase
{
/**
*/
public function testBasic(): void
{
$wmd = bean('wsMsgDispatcher');
$this->assertNotEmpty($wmd);
}

public function testDispatch(): void
{
$wmd = bean('wsMsgDispatcher');
$this->assertNotEmpty($wmd);
}

/**
*/
public function testCustomDispatch(): void
{
$wmd = bean('wsMsgDispatcher');
$this->assertNotEmpty($wmd);
}
}

0 comments on commit 5ea10ec

Please sign in to comment.