Skip to content

Commit 4911c72

Browse files
committed
Auto open browser window when running example
1 parent d925030 commit 4911c72

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
"ratchet/rfc6455": "^0.2.3",
2626
"react/http": "^0.8"
2727
},
28+
"require-dev":{
29+
"react/child-process": "^0.5.0"
30+
},
2831
"autoload": {
2932
"psr-4": {
3033
"Voryx\\WebSocketMiddleware\\": "src/"

example/chat_ws_server.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
$loop = Factory::create();
1616

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

1920
$broadcast = new ThroughStream();
2021

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

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

73+
openWebPage($loop, 'http://' . $uri);
74+
7275
$loop->run();
76+
77+
function openWebPage($loop, $url)
78+
{
79+
$os = strtolower(php_uname(PHP_OS));
80+
81+
if (strpos($os, 'darwin') !== false) {
82+
$open = 'open';
83+
} elseif (strpos($os, 'linux') !== false) {
84+
$open = 'xdg-open';
85+
} else {
86+
echo "Can't open your browser, you'll have to manually navigate to {$url}", PHP_EOL;
87+
return;
88+
}
89+
90+
$process = new React\ChildProcess\Process("{$open} {$url}");
91+
92+
try {
93+
$process->start($loop);
94+
} catch (Exception $e) {
95+
echo $e->getMessage(), PHP_EOL;
96+
}
97+
}

0 commit comments

Comments
 (0)