PHPackages                             mwyd/psockets - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. mwyd/psockets

AbandonedArchivedLibrary[HTTP &amp; Networking](/categories/http)

mwyd/psockets
=============

Synchronous PHP websocket client

0.11(5y ago)022MITPHPPHP ^8.0

Since Apr 8Pushed 4y ago1 watchersCompare

[ Source](https://github.com/mwyd/pSockets-client)[ Packagist](https://packagist.org/packages/mwyd/psockets)[ RSS](/packages/mwyd-psockets/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)DependenciesVersions (4)Used By (0)

pSockets-client
===============

[](#psockets-client)

[![Autobahn Testsuite](https://camo.githubusercontent.com/9ee59f03c0e8b8f9145a005f06327fb29e18f8b9a841478457b2fdb468ca365f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4175746f6261686e2d70617373696e672d627269676874677265656e2e737667)](http://psockets.mwyd.me/)

Synchronous PHP websocket client

Requirements
============

[](#requirements)

- PHP ^8.0
- Command prompt or terminal access

Installation
============

[](#installation)

It is recommended to install package via composer

```
composer require mwyd/psockets

```

Documentation
=============

[](#documentation)

```
abstract class WsClient
{
    /**
    * @var Logger $logger
    */
    protected Logger $logger;

    /**
    * @param string $address valid url
    * @param array $config default values => [
    *   'FLAGS'                 => null,
    *   'CONTEXT'               => null,
    *   'LOG_LEVEL'             => 1,
    *   'BUFFER_SIZE'           => 8192,
    *   'CONNECT_TIMEOUT'       => 10,
    *   'HANDSHAKE_TIMEOUT'     => 2,
    *   'ADDITIONAL_HEADERS'    => []
    * ]
    * @param array['FLAGS']?int alias of socket_create_client flags parameter
    * @param array['CONTEXT']mixed alias of socket_create_client context parameter
    * @param array['CONNECT_TIMEOUT']?float alias of socket_create_client timeout parameter
    * @param array['LOG_LEVEL']int 0 => logs disabled, 1 => connect, disconnect, 2 => connect, disconnect, messages
    * @param array['HANDSHAKE_TIMEOUT']int seconds
    */
    public function __construct(string $address, array $config);

    /**
    *  Method to be called when the connection is opened
    */
    abstract protected function onOpen() : void;

    /**
    * Method to be called when a message is received from the server
    */
    abstract protected function onMessage(WsMessage $message) : void;

    /**
    * Method to be called when the connection is closed
    */
    abstract protected function onClose() : void;

    /**
    * Writes data to buffer
    * @param string $message message to be written
    * @param bool $isBinary is the message binary
    * @param int $fragmentSize when value is greater than 0 and less than message length data will be sent in many frames otherwise single frame will be sent
    */
    public function send(string $message, bool $isBinary = false, int $fragmentSize = 0) : void;

    /**
    * Closes the connection
    */
    public function close() : void;

    /**
    * Returns current connection state
    * @return int connection state 0 => CST_CONNECTING, 1 => CST_OPEN, 2 => CST_CLOSING, 3 => CST_CLOSED
    */
    public function getState() : int;

    /**
    * Returns path
    * @return string path
    */
    public function getPath() : string;

    /**
    * Returns handshake status
    * @return bool success
    */
    public function getHandshake() : bool;

    /**
    * Returns local address
    * @return string local address
    */
    public function getAdress() : string;

    /**
    * Returns amount of bytes in a specified buffer
    * @param string $type buffer type 'r' => read buffer, 'w' => write buffer
    * @return int amount of bytes
    */
    public function getBufferLen(string $type) : int;

    /**
    * Runs the main loop
    */
    public function run() : void;
}

class WsMessage
{
    public function __construct(string $message, bool $isBinary);

    public function isBinary() : bool;

    /**
    * @throws \JsonException
    */
    public function json() : mixed;

    public function __toString() : string;
}

class Logger
{
    public function __construct(int $logLevel);

    public function log(string $msg, int $level = 0) : void;

    public static function warn(string $msg) : void;

    public static function err(string $msg) : void;
}
```

Usage example
=============

[](#usage-example)

```
