PHP code example of phasync / server

1. Go to this page and download the library: Download phasync/server library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phasync / server example snippets


// Starts listening to port 8080 on localhost
Server::serve('tcp://127.0.0.1:8080', function($stream, $peer) {
    phasync::readable($stream);     // Wait for data from the client
    fread($stream, 65536);
    phasync::writable($stream);     // Wait until the client is ready to receive data
    fwrite($stream, 
        "HTTP/1.1 200 Ok\r\n".
        "Connection: close\r\n".
        "Content-Length: 13\r\n".
        "\r\n".
        "Hello, $peer!"
    );
    fclose($stream);
});

Server::serve('tcp://127.0.0.1:8080', function($stream, $peer) {
    // ... handle TCP connection
});

Server::serve('udp://127.0.0.1:9000', function(Server $server) {
    while (true) {
        $data = $server->recvfrom(65536, 0, $peer);
        if ($data !== false) {
            $server->sendto("Response Data", 0, $peer);
        }
    }
});