Skip to content

Commit

Permalink
修复部分服务器事件监听问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Jul 30, 2018
1 parent c9c3367 commit 188fdea
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Server/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __construct($name, $config, $isSubServer = false)
}
if(!empty($config['configs']))
{
$this->swooleServer->set($config['configs']);
($this->swoolePort ?? $this->swooleServer)->set($config['configs']);
}
$this->bindEvents();
}
Expand Down
6 changes: 4 additions & 2 deletions src/Server/Http/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ protected function getServerInitConfig()
*/
protected function __bindEvents()
{
$this->swooleServer->on('request', function(\swoole_http_request $swooleRequest, \swoole_http_response $swooleResponse){
$server = $this->swoolePort ?? $this->swooleServer;

$server->on('request', function(\swoole_http_request $swooleRequest, \swoole_http_response $swooleResponse){
$request = new Request($this, $swooleRequest);
$response = new Response($this, $swooleResponse);
$this->trigger('request', [
Expand All @@ -66,7 +68,7 @@ protected function __bindEvents()
], $this, RequestEventParam::class);
});

$this->swooleServer->on('close', function(\swoole_http_server $server, $fd, $reactorID){
$server->on('close', function(\swoole_http_server $server, $fd, $reactorID){
$this->trigger('close', [
'server' => $this,
'fd' => $fd,
Expand Down
12 changes: 7 additions & 5 deletions src/Server/TcpServer/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,17 @@ protected function getServerInitConfig()
*/
protected function __bindEvents()
{
$this->swooleServer->on('connect', function(\swoole_server $server, $fd, $reactorID){
$server = $this->swoolePort ?? $this->swooleServer;

$server->on('connect', function(\swoole_server $server, $fd, $reactorID){
$this->trigger('connect', [
'server' => $this,
'fd' => $fd,
'reactorID' => $reactorID,
], $this, ConnectEventParam::class);
});

$this->swooleServer->on('receive', function(\swoole_server $server, $fd, $reactorID, $data){
$server->on('receive', function(\swoole_server $server, $fd, $reactorID, $data){
$this->trigger('receive', [
'server' => $this,
'fd' => $fd,
Expand All @@ -86,22 +88,22 @@ protected function __bindEvents()
], $this, ReceiveEventParam::class);
});

$this->swooleServer->on('close', function(\swoole_server $server, $fd, $reactorID){
$server->on('close', function(\swoole_server $server, $fd, $reactorID){
$this->trigger('close', [
'server' => $this,
'fd' => $fd,
'reactorID' => $reactorID,
], $this, CloseEventParam::class);
});

$this->swooleServer->on('BufferFull', function(\swoole_server $server, $fd){
$server->on('BufferFull', function(\swoole_server $server, $fd){
$this->trigger('bufferFull', [
'server' => $this,
'fd' => $fd,
], $this, BufferEventParam::class);
});

$this->swooleServer->on('BufferEmpty', function(\swoole_server $server, $fd){
$server->on('BufferEmpty', function(\swoole_server $server, $fd){
$this->trigger('bufferEmpty', [
'server' => $this,
'fd' => $fd,
Expand Down
4 changes: 3 additions & 1 deletion src/Server/UdpServer/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ protected function getServerInitConfig()
*/
protected function __bindEvents()
{
$this->swooleServer->on('packet', function(\swoole_server $server, $data, $clientInfo){
$server = $this->swoolePort ?? $this->swooleServer;

$server->on('packet', function(\swoole_server $server, $data, $clientInfo){
$this->trigger('packet', [
'server' => $this,
'data' => $data,
Expand Down
8 changes: 5 additions & 3 deletions src/Server/WebSocket/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ protected function getServerInitConfig()
*/
protected function __bindEvents()
{
$this->swooleServer->on('handShake', function(\swoole_http_request $swooleRequest, \swoole_http_response $swooleResponse){
$server = $this->swoolePort ?? $this->swooleServer;

$server->on('handShake', function(\swoole_http_request $swooleRequest, \swoole_http_response $swooleResponse){
$request = new Request($this, $swooleRequest);
$response = new Response($this, $swooleResponse);
$this->trigger('handShake', [
Expand All @@ -79,14 +81,14 @@ protected function __bindEvents()
], $this, HandShakeEventParam::class);
});

$this->swooleServer->on('message', function (\swoole_websocket_server $server, \swoole_websocket_frame $frame) {
$server->on('message', function (\swoole_websocket_server $server, \swoole_websocket_frame $frame) {
$this->trigger('message', [
'server' => $this,
'frame' => $frame,
], $this, MessageEventParam::class);
});

$this->swooleServer->on('close', function(\swoole_http_server $server, $fd, $reactorID){
$server->on('close', function(\swoole_http_server $server, $fd, $reactorID){
$this->trigger('close', [
'server' => $this,
'fd' => $fd,
Expand Down

0 comments on commit 188fdea

Please sign in to comment.