MAPG-203 add class for multiplayer internal connection (PHP-NodeJS TCP)
This commit is contained in:
parent
ed343f2359
commit
79490a0616
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