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

This commit is contained in:
bence committed 2021-03-20 19:45:21 +01:00
1 parent ed343f2359
commit 79490a0616
1 file changed
+24
+24
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);
}
}
}