Skip to content
This repository has been archived by the owner on Dec 31, 2018. It is now read-only.

steelbrain/HHVM-Async-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HHVM-Async-Server

Async Socket Server Experiment in HHVM's HackLang. it makes use of HackLang's built-in async functions.

Usage

$Server = new Server('localhost', 9098);
$Server->Listen(async function(resource $Client){
  await Server::OnResponse($Client, async function(string $Data, resource $Client){
    echo "Client Sent Data: $Data\n";
  });
});

Usage.ServerOnly

When You only want the server, You can do $Server->Listen(...)->getWaitHandle()->join(). This is a blocking operation, which means that It won't let the program get to the next line. It'll immediately start waiting for clients to connect to it. You can also store the wait handle in a variable and join it at the end of your program, like

$WaitHandle = $Server->Listen(...)->getWaitHandle();
... Do Stuff Here
$WaitHandle->join();

Usage.ServerSidedApp

If you have a server sided app, then it of course does a lot of other stuff than just listening on an http port. In that case, You can execute two async functions and join them both, for example.

class MyServerSidedApp{
  public Server $Server;
  public function __construct(){
    $this->Server = $Server = new Server('localhost', 9098);
  }
  public async function Init():Awaitable<void>{
    // Do the regular app stuff here..
  }
}
$App = new MyServerSidedApp();
GenArrayWaitHandle::create([
  $App->Server->Listen(async function(resource $Client){
    await Server::OnResponse($Client, async function(string $Data, resource $Client){
      echo "Client Sent Data: $Data\n";
    });
  })->getWaitHandle(), $App->Init()]
)->join();
// This way your program and the HTTP Server and App will run co-currently

As you can see above, the code above looks un-clean, to fix that, Server class contains a method ListenInCoop just for this purpose. Here's how you can use it.

class MyServerSidedApp{
  public Server $Server;
  public function __construct(){
    $this->Server = $Server = new Server('localhost', 9098);
  }
  public async function OnConnect(resource $Client):Awaitable<void>{
    await Server::OnResponse($Client, async function(string $Data, resource $Client){
      echo "Client Sent Data: $Data\n";
    });
  }
  public async function Init():Awaitable<void>{
    await $this->Server->ListenInCoop(inst_meth($this, 'OnConnect'), async function(Server $Instance):Awaitable<void>{
      ... Do Stuff
      if(ShouldCloseServer()){
        $Instance->Close();
      }
    });
  }
}
$App = new MyServerSidedApp();
$App->Init()->getWaitHandle()->join();

License

This Project is licensed under the terms of MIT License.

About

Async Socket Server Experiment in HHVM's HackLang

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages