forked from joewalnes/websocketd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples for hack lang (joewalnes#337)
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
This examples directory shows some examples written in [Hack](https://hacklang.org). | ||
|
||
### Requirements : | ||
|
||
- [HHVM](https://github.com/facebook/hhvm) 3.30+ | ||
- [HSL ( Hack Standard Library )](https://github.com/hhvm/hsl) 3.30+ | ||
- [HSL Experimental](https://github.com/hhvm/hsl-experimental) 3.30+ | ||
|
||
You can also test the command files by running from the command line : | ||
|
||
``` | ||
$ hhvm count.hh | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
9 | ||
10 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/hhvm | ||
<?hh // strict | ||
|
||
use namespace HH\Lib\Str; | ||
use function HH\Lib\Experimental\IO\request_output; | ||
use function usleep; | ||
|
||
// Simple example script that counts to 10 at ~2Hz, then stops. | ||
|
||
<<__EntryPoint>> | ||
async function count_to_ten(): Awaitable<noreturn> { | ||
$output = request_output(); | ||
for ($count = 1; $count <= 10; $count++) { | ||
await $output->writeAsync( | ||
Str\format("%d\n",$count) | ||
); | ||
|
||
// usleep is builtin, it is not an async builtin - so it also must block the main request thread | ||
usleep(500000); | ||
} | ||
|
||
// flush output | ||
await $output->flushAsync(); | ||
|
||
exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/hhvm | ||
<?hh // strict | ||
|
||
use namespace HH\Lib\Str; | ||
use function HH\Lib\Experimental\IO\request_output; | ||
|
||
<<__EntryPoint>> | ||
async function dumpEnv(): Awaitable<noreturn> { | ||
// Standard CGI(ish) environment variables, as defined in | ||
// http://tools.ietf.org/html/rfc3875 | ||
$names = keyset[ | ||
'AUTH_TYPE', | ||
'CONTENT_LENGTH', | ||
'CONTENT_TYPE', | ||
'GATEWAY_INTERFACE', | ||
'PATH_INFO', | ||
'PATH_TRANSLATED', | ||
'QUERY_STRING', | ||
'REMOTE_ADDR', | ||
'REMOTE_HOST', | ||
'REMOTE_IDENT', | ||
'REMOTE_PORT', | ||
'REMOTE_USER', | ||
'REQUEST_METHOD', | ||
'REQUEST_URI', | ||
'SCRIPT_NAME', | ||
'SERVER_NAME', | ||
'SERVER_PORT', | ||
'SERVER_PROTOCOL', | ||
'SERVER_SOFTWARE', | ||
'UNIQUE_ID', | ||
'HTTPS' | ||
]; | ||
|
||
/* HH_IGNORE_ERROR[2050] using global variable */ | ||
$server = dict($_SERVER); | ||
|
||
$ouput = request_output(); | ||
|
||
foreach($names as $name) { | ||
await $output->writeAsync( | ||
Str\format("%s = %s\n", $name, $server[$name] ?? '<unset>') | ||
); | ||
} | ||
|
||
// Additional HTTP headers | ||
foreach($server as $k => $v) { | ||
if ($k is string && Str\starts_with($k, 'HTTP_')) { | ||
await $output->writeAsync( | ||
Str\format("%s = %s\n", $k, $v as string) | ||
); | ||
} | ||
} | ||
|
||
// flush output | ||
await $output->flushAsync(); | ||
|
||
exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/hhvm | ||
<?hh // strict | ||
|
||
use namespace HH\Lib\Str; | ||
use namespace HH\Lib\Experimental\IO; | ||
|
||
<<__EntryPoint>> | ||
async function greeter(): Awaitable<noreturn> { | ||
// For each line FOO received on STDIN, respond with "Hello FOO!". | ||
$input = IO\request_input(); | ||
$output = IO\request_output(); | ||
while(!$input->isEndOfFile()) { | ||
await $ouput->writeAsync( | ||
Str\format("Hello %s!\n", await $input->readLineAsync()) | ||
); | ||
} | ||
|
||
// flush output | ||
await $output->flushAsync(); | ||
|
||
exit(0); | ||
} |