-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/websocket-server/test/unit/WsMessageDispatcherTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |