Skip to content

Commit

Permalink
Auto open browser window when running example
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwdan committed Dec 12, 2017
1 parent d925030 commit 4911c72
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"ratchet/rfc6455": "^0.2.3",
"react/http": "^0.8"
},
"require-dev":{
"react/child-process": "^0.5.0"
},
"autoload": {
"psr-4": {
"Voryx\\WebSocketMiddleware\\": "src/"
Expand Down
25 changes: 25 additions & 0 deletions example/chat_ws_server.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
$loop = Factory::create();

$frontend = file_get_contents(__DIR__ . '/test.html');
$uri = '127.0.0.1:4321';

$broadcast = new ThroughStream();

Expand Down Expand Up @@ -69,4 +70,28 @@ function (ServerRequestInterface $request, callable $next) use ($frontend) {

$server->listen(new \React\Socket\Server('127.0.0.1:4321', $loop));

openWebPage($loop, 'http://' . $uri);

$loop->run();

function openWebPage($loop, $url)
{
$os = strtolower(php_uname(PHP_OS));

if (strpos($os, 'darwin') !== false) {
$open = 'open';
} elseif (strpos($os, 'linux') !== false) {
$open = 'xdg-open';
} else {
echo "Can't open your browser, you'll have to manually navigate to {$url}", PHP_EOL;
return;
}

$process = new React\ChildProcess\Process("{$open} {$url}");

try {
$process->start($loop);
} catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}
}

0 comments on commit 4911c72

Please sign in to comment.