PHP code example of alexya-framework / sockswork

1. Go to this page and download the library: Download alexya-framework/sockswork 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/ */

    

alexya-framework / sockswork example snippets




$SocksWork = new SocksWork("localhost", 8080, 100); // Connects to localhost:8080 and sets a timeout of 100ms



$SocksWork->send((binary) "Hello world!");
echo $SocksWork->response; // Response to the packet

$packet = new PacketBuilder();
$packet->writeString("message", "Hello World!");

$SocksWork->send($packet);

echo $packet->readString("response"); // Response to the packet



$packet = new PacketBuilder();

$packet->writeShort("id", 1);
$packet->writeString("name", "test");

$SocksWork->send($packet);

$id   = $packet->readShort("id");
$name = $packet->readString("name");



class SetUserName extends PacketBuilder
{
    private $_id = 1;

    public $id   = -1;
    public $name = "";

    public function onInstance(string $name)
    {
        $this->_encoder->writeShort("id", $this->_id);
        $this->_encoder->writeString("name", $name);
    }

    public function onResponse()
    {
        $this->id   = $this->_ecoder->readShort("id");
        $this->name = $this->_encoder->readString("name");
    }
}

$packet = new SetUserName("test");

$SocksWork->send($packet);

$id   = $packet->id;
$name = $packet->name;



class Packet extends PacketBuilder
{
    public function onInstance()
    {
        foreach(func_get_args() as $key => $val) {
            echo "{$key} => {$value}\n";
        }
    }
}

$packet = new Packet(1, "test", "foo");
// Output:
// 0 => 1
// 1 => test
// 2 => foo

$packet = new Packet(new \Alexya\SocksWork\Encoders\String(), 1, "test");
// Output:
// 0 => 1
// 1 => test