feature/MAPG-203-initial-multiplayer-implementation #8
24
src/Multi/MultiConnector.php
Normal file
24
src/Multi/MultiConnector.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user