Skip to content

Commit

Permalink
Improved server messages (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoreram authored Mar 11, 2020
1 parent 0401600 commit 9bdaaa8
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 16 deletions.
5 changes: 0 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ jobs:
steps:
- checkout

- run:
name: Install PHPUnit
command: |
composer require phpunit/phpunit:7.5.17 --no-update
- run:
name: Run tests / Symfony 5^0
command: |
Expand Down
24 changes: 24 additions & 0 deletions Connection/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php


namespace Drift\Websocket\Connection;

use Ratchet\ConnectionInterface;

/**
* Class Connection
*/
class Connection
{
/**
* Get connection hash
*
* @param ConnectionInterface $connection
*
* @return string
*/
public static function getConnectionHash(ConnectionInterface $connection) : string
{
return substr(md5(spl_object_hash($connection)), 0, 13);
}
}
18 changes: 9 additions & 9 deletions Connection/WebsocketApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public function onOpen(ConnectionInterface $connection)

if ($this->outputPrinter) {
(new ConsoleWebsocketMessage(sprintf(
'Connection %s opened on route %s',
spl_object_hash($connection),
'Connection %s - %s - Opened connection',
Connection::getConnectionHash($connection),
$this->name
), '~', true))->print($this->outputPrinter);
}
Expand All @@ -105,8 +105,8 @@ public function onClose(ConnectionInterface $connection)

if ($this->outputPrinter) {
(new ConsoleWebsocketMessage(sprintf(
'Connection %s closed from route %s',
spl_object_hash($connection),
'Connection %s - %s - Closed connection',
Connection::getConnectionHash($connection),
$this->name
), '~', true))->print($this->outputPrinter);
}
Expand All @@ -122,8 +122,8 @@ public function onError(ConnectionInterface $connection, Exception $exception)

if ($this->outputPrinter) {
(new ConsoleWebsocketMessage(sprintf(
'Connection %s throw error on route %s',
spl_object_hash($connection),
'Connection %s - %s - Error thrown',
Connection::getConnectionHash($connection),
$this->name
), '~', true))->print($this->outputPrinter);
}
Expand All @@ -139,10 +139,10 @@ public function onMessage(ConnectionInterface $from, $message)

if ($this->outputPrinter) {
(new ConsoleWebsocketMessage(sprintf(
'Message from connection %s on route %s - %s',
spl_object_hash($from),
'Connection %s - %s - Messaged "%s"',
Connection::getConnectionHash($from),
$this->name,
$message
trim($message, " \ \t\n\r\0\x0B")
), '~', true))->print($this->outputPrinter);
}
}
Expand Down
1 change: 0 additions & 1 deletion Console/ConnectToWebsocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
});

$stdio->on('data', function ($data) use ($connection, $stdio) {
echo 'LOL';
$connection->send($data);
});
})
Expand Down
19 changes: 19 additions & 0 deletions Console/WebsocketHeaderPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public static function print(
$outputPrinter->printHeaderLine("Port: $port");
$outputPrinter->printHeaderLine("Environment: {$input->getOption('env')}");
$outputPrinter->printHeaderLine('Debug: '.($input->getOption('no-debug') ? 'disabled' : 'enabled'));
$outputPrinter->printHeaderLine('Routes: '.(!empty($input->getOption('route'))
? implode(', ', static::getPlainRoutes($input))
: 'disabled'
));
$outputPrinter->printHeaderLine('Exchanges subscribed: '.(!empty($input->getOption('exchange'))
? implode(', ', static::getPlainExchanges($input))
: 'disabled'
Expand All @@ -58,6 +62,21 @@ public static function print(
$outputPrinter->printLine();
}

/**
* @param InputInterface $input
*
* @return array
*/
public static function getPlainRoutes(InputInterface $input): array
{
$array = [];
foreach ($input->getOption('route') as $route) {
$array[] = trim($route);
}

return $array;
}

/**
* @param InputInterface $input
*
Expand Down
9 changes: 8 additions & 1 deletion Tests/RouteFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ protected static function decorateConfiguration(array $configuration): array
'main' => [
'path' => '/',
],
'another' => [
'path' => '/another/',
],
],
];

Expand All @@ -61,8 +64,12 @@ public function testRouteConnection()
await($promise, $loop);
sleep(1);

$this->assertContains('opened on route main', $websocketServer->getOutput());
$this->assertContains('Opened connection', $websocketServer->getOutput());
$this->assertContains('TestEvent', $websocketServer->getOutput());
$this->assertContains('Exchanges subscribed: events', $websocketServer->getOutput());
$this->assertContains('Routes: main, another', $websocketServer->getOutput());
$this->assertContains('Port: 8001', $websocketServer->getOutput());
$this->assertContains('Host: localhost', $websocketServer->getOutput());

$this->assertContains('Opened connection', $conn1->getOutput());
$this->assertContains(TestEvent::class, $conn1->getOutput());
Expand Down
1 change: 1 addition & 0 deletions Tests/WebsocketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ protected function createSocketServer(string $port): Process
'websocket:run',
'localhost:'.$port,
'--route=main',
'--route=another',
'--exchange=events',
]);
}
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},

"require-dev": {
"phpunit/phpunit": "7.5.17",
"drift/amqp-bundle": "0.1.*, >=0.1.2",
"symfony/process": "^5.0"
},
Expand Down

0 comments on commit 9bdaaa8

Please sign in to comment.