Skip to content

Commit

Permalink
answer
Browse files Browse the repository at this point in the history
  • Loading branch information
RofFlexor committed Sep 15, 2023
1 parent 738892b commit 2f0f1e9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/Services/ClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public function __construct()
*/
public function start() {
$socket = new Socket('unix://'.(new Config())->getSocketPath(), $this->loop);
$socket->on('close', function() {
printf("Server has closed the connection!\n");
$this->loop->stop();
});

$socket->on('data', function ($socket, $data) {
printf("%s\n", $data);
});
$this->loop->addPeriodicTimer(1, function() use($socket) {
$input = rtrim(fgets(STDIN));
if(!empty($input)) {
$socket->write($input);
$socket->read();
}
});

Expand Down
11 changes: 4 additions & 7 deletions src/Services/ServerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ public function __construct()
public function start() {
$server = new SocketListener('unix://'.(new Config())->getSocketPath(), $this->loop);
$server->on('connect', function($server, SocketInterface $client) {
printf("New connection #%s from %s!\n", $res = $client->getResourceId(), $client->getLocalAddress());

$client->on('data', function($client, $data) use(&$buffer) {
printf("Received message=\"%s\"\n", $data);
});
$client->on('close', function() use($res) {
printf("Closed connection #$res\n");
$client->write("Received message=$data\n");
});
});
$server->start();
$this->loop->onStart(function() use($server) {
$server->start();
});
$this->loop->start();
}

Expand Down

0 comments on commit 2f0f1e9

Please sign in to comment.