MAPG-203 add class for multiplayer internal connection (PHP-NodeJS TCP)

This commit is contained in:
Bence Pőcze 2021-03-19 22:59:57 +01:00
parent ed343f2359
commit 79490a0616
Signed by: bence
GPG Key ID: AA52B11A3269D1C1

View File

@ -0,0 +1,24 @@
<?php namespace MapGuesser\Multi;
class MultiConnector
{
public function sendMessage(string $func, array $args = []): void
{
$message = json_encode([
'func' => $func,
'args' => $args
]);
$connection = fsockopen($_ENV['MULTI_INTERNAL_HOST'], $_ENV['MULTI_INTERNAL_PORT']);
fwrite($connection, $message);
$response = '';
while (!feof($connection)) {
$response .= fgets($connection);
}
fclose($connection);
if ($response !== 'OK') {
throw new \Exception('Sending message failed with response: ' . $response);
}
}
}