Open juslintek opened 4 years ago
It seems, that you have to terminate connection every 20 seconds and exit process, then it works. Then the process is instantiated and it's good. But it is a kind of nasty solution. I still need to experiment with timeouts. At the moment timeout is 20
on $frame = $client->recv(20);
. After I execute:
class TestProcess implements CustomProcessInterface
{
private static bool $quit = false;
public static function callback(Server $swoole, Process $process): void
{
$client = new Client(
'echo.websocket.org',
443
);
$client->set([
'reconnect' => 3,
'timeout' => 30,
'defer' => true,
]);
$client->setHeaders([
'Host' => 'echo.websocket.org',
'User-Agent' => 'Chrome/49.0.2587.3',
'Accept' => 'application/json',
'Accept-Encoding' => 'gzip',
]);
$upgraded = $client->upgrade('/');
if (!$upgraded) {
\Log::info('WS Client process: failed to connect');
return;
}
\Log::info('WS Client process: connected');
while (!self::$quit) {
\Log::debug(
'WS Client process: is listening',
self::getProcessDetails($client, $process, $swoole)
);
$frame = $client->recv(20);
if ($client->errCode) {
\Log::error($client->errMsg, self::getProcessDetails($client, $process, $swoole));
$client->close();
$process->exit($client->errCode);
self::$quit = true;
}
if ($frame instanceof Frame && $frame->data !== null) {
\Log::debug('WS Client process: received frame', (array) $frame);
self::processFrame($frame, $process);
Coroutine::sleep(0.5);
}
}
}
public static function getProcessDetails(Client $client, Process $process, Server $server): array
{
return [
'body' => $client->getBody(),
'connected' => $client->connected,
'errorCode' => $client->errCode,
'errorMsg' => $client->errMsg,
'statusCode' => $client->statusCode,
'socketName' => $client->getsockname(),
'peerName' => $client->getpeername(),
'pid' => $process->pid,
'id' => $process->id,
'queueId' => $process->msgQueueId,
'queueKey' => $process->msgQueueKey,
'lastServerError' => $server->getLastError(),
];
}
public static function processFrame(Frame $frame, Process $process): void
{
try {
$data = json_decode($frame->data, true, 512, JSON_THROW_ON_ERROR | JSON_OBJECT_AS_ARRAY);
\Log::debug('Message: ', $data);
} catch (\JsonException $jsonException) {
\Log::error(
$frame->data,
[
'previousError' => $jsonException->getMessage()
. ' at ' . $jsonException->getFile() .
':' . $jsonException->getLine(),
]
);
$process->exit($jsonException->getCode());
}
}
public static function onReload(Server $swoole, Process $process): void
{
\Log::info('WS Client process: reloading');
self::$quit = true;
}
public static function onStop(Server $swoole, Process $process): void
{
\Log::info(
'WS Client process: stopped',
[
$swoole->getLastError(),
]
);
self::$quit = true;
$process->exit(0);
}
}
What is the best approach?
Your software version (Screenshot of your startup)
Detail description about this issue(error/log)
reproducible
code blocks andsteps
Steps to reproduce the same as in that issue. https://github.com/hhxsv5/laravel-s/issues/258
Just changed heartbeat in config these:
And using sock instead of tcp.
define('SWOOLE_SOCK_UNIX_STREAM', 5);
Nginx config:
Supervisor config:
P.S. This doesn't happen on Docker. This happens only in Azure VPS.