Merge pull request #1718 from jobinli/hotfix#socketio-trigger-connect

socketIo组件在onOpen中促发connect事件
This commit is contained in:
谷溪 2020-05-12 16:54:08 +08:00 committed by GitHub
commit a7e3b90ac6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -206,17 +206,13 @@ class SocketIO implements OnMessageInterface, OnOpenInterface, OnCloseInterface
];
$server->push($request->fd, Engine::OPEN . json_encode($data)); //socket is open
$server->push($request->fd, Engine::MESSAGE . Packet::OPEN); //server open
$this->dispatchEventInAllNamespaces($request->fd, 'connect');
}
public function onClose(Server $server, int $fd, int $reactorId): void
{
$all = SocketIORouter::list();
if (! array_key_exists('forward', $all)) {
return;
}
foreach (array_keys($all['forward']) as $nsp) {
$this->dispatch($fd, $nsp, 'disconnect', null);
}
$this->dispatchEventInAllNamespaces($fd, 'disconnect');
}
/**
@ -301,4 +297,15 @@ class SocketIO implements OnMessageInterface, OnOpenInterface, OnCloseInterface
$this->addCallback($ackId, $channel, $timeout);
}, ]);
}
private function dispatchEventInAllNamespaces(int $fd, string $event)
{
$all = SocketIORouter::list();
if (! array_key_exists('forward', $all)) {
return;
}
foreach (array_keys($all['forward']) as $nsp) {
$this->dispatch($fd, $nsp, $event, null);
}
}
}