2017 © Pedro Peláez
 

library php-websocket

PHP WebSocket server and client library

image

hemiframe/php-websocket

PHP WebSocket server and client library

  • Wednesday, July 4, 2018
  • by heminei
  • Repository
  • 2 Watchers
  • 8 Stars
  • 299 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 2 Open issues
  • 3 Versions
  • 47 % Grown

The README.md

Simple and Powerfull PHP WebSocket Server and Client

Simple and powerful PHP WebSocket implementation for PHP, (*1)

Supports

  • multiple paths
  • check-origin
  • limit clients
  • sends status codes on close connections
  • sends custom headers

Requirements

PHP >= 7.0, (*2)

Installation

Enter in composer.json, (*3)

{
    "require": {
        "hemiframe/php-websocket": "~1.7"
    }
}

or run in console: php composer.phar require hemiframe/php-websocket, (*4)

How to run demo?

Open CMD or terminal and enter, (*5)

php demo/server.php

After that you can open demo/index.html in your browser, (*6)

Server example

require_once('vendor/autoload.php');

$socket = new \HemiFrame\Lib\WebSocket\WebSocket("localhost", 8080);
$socket->on("receive", function($client, $data) use($socket) {
    foreach ($socket->getClients() as $item) {
        /* @var $item \HemiFrame\Lib\WebSocket\Client */
        if ($item->id != $client->id) {
            $socket->sendData($item, $data);
        }
    }
});
$socket->startServer();

Client example

require_once('vendor/autoload.php');

$socket = new \HemiFrame\Lib\WebSocket\WebSocket('localhost', 8080);
$socket->on("receive", function($client, $data) use($socket) {
});
$client = $socket->connect();
if ($client) {
    $socket->sendData($client, "Message");
    $socket->disconnectClient($client);
}

Documentation

Events:

$socket->on("connect", function(\HemiFrame\Lib\WebSocket\Client $client) {
});

$socket->on("receive", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});

$socket->on("send", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});

$socket->on("ping", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});

$socket->on("pong", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});

$socket->on("disconnect", function(\HemiFrame\Lib\WebSocket\Client $client, $statusCode, $reason) {
});

$socket->on("error", function($socket, $client, $phpError, $errorMessage, $errorCode) {
});

Methods

$socket->getSocket();

/**
* Get connected clients
* @return array
*/
$socket->getClients();

/**
* Get connected clients, filtered by path
* @return array
*/
$socket->getClientsByPath($path = "/");

$socket->getAllowedOrigins();

$socket->setAllowedOrigins($allowedOrigins);

$socket->getMaxClients();

$socket->setMaxClients($maxClients);

$socket->getBufferSize();

$socket->setBufferSize($bufferSize);

$socket->getUserAgent();

$socket->setUserAgent($userAgent);

$socket->setEnableLogging($enableLogging);

$socket->create();

$socket->close();

$socket->setOption($socket, $level, $optname, $optval);

$socket->bind($address, $port = 0);

$socket->listen($backlog = 0);

$socket->select(&$read, &$write, &$except, $backlog, $tv_usec = 0);

$socket->accept();

$socket->write($socket, $message = null);

$socket->recv($socket);

$socket->read($socket);

/**
* Get last socket error code
* Alias of socket_last_error
* @return int
*/
$socket->getLastErrorCode();

/**
* Get last socket error message
* @return string
*/
$socket->getLastErrorMessage();

/**
* Connect to websocket server. Send and validate handshake request.
* @param string $path Send custom path to server
* @param string $origin Send custom origin host to server
* @return mixed Return client object when is successfully connected to server or FALSE when is not.
*/
$socket->connect($path = "/", $origin = null);

/**
* Run WebSocket server.
* Create main socket.
* Bind address and port.
* Listens for a connection on a socket.
* Call main processing loop
*/
$socket->startServer();

/**
* Disconnect all clients and close main socket
*/
$socket->stopServer();

/**
* Main processing loop. Check for new clients, incomming data, diconnected clients
*/
$socket->loop();

/**
* Trigger event
* @param string $name Event name
* @param array $arguments Arguments for sending
*/
$socket->trigger($name, $arguments);

/**
* Subscribe for event.
* @param string $name Event name
* @param function $function Callback function
*/
$socket->on($name, $function);

/**
* Subscribe on event. Call only first time
* @param string $name Event name
* @param function $function Callback function
*/
$socket->one($name, $function);

/**
* Send data to client
* @param object $client
* @param string $data
*/
$socket->sendData($client, $data);

/**
* Disconnect client. You can send custom status code and reason
* @param object $client
* @param int $statusCode
* @param string $reason
* @return self
*/
$socket->disconnectClient($client, $statusCode = self::STATUS_CLOSE_NORMAL, $reason = null);

The Versions

04/07 2018

dev-master

9999999-dev

PHP WebSocket server and client library

  Sources   Download

MIT

The Requires

  • php >=7.0

 

php server client websocket websockets

04/07 2018

1.5.3

1.5.3.0

PHP WebSocket server and client library

  Sources   Download

MIT

The Requires

  • php >=7.0

 

php server client websocket websockets

24/12 2017

1.5.2

1.5.2.0

PHP WebSocket server and client library

  Sources   Download

MIT

The Requires

  • php >=7.0

 

php server client websocket websockets