1. Go to this page and download the library: Download arthurkushman/php-wss 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/ */
arthurkushman / php-wss example snippets
use WSSC\Contracts\ConnectionContract;
use WSSC\Contracts\WebSocket;
use WSSC\Exceptions\WebSocketException;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class ServerHandler extends WebSocket
{
/*
* if You need to parse URI context like /messanger/chat/JKN324jn4213
* You can do so by placing URI parts into an array - $pathParams, when Socket will receive a connection
* this variable will be appropriately set to key => value pairs, ex.: ':context' => 'chat'
* Otherwise leave $pathParams as an empty array
*/
public $pathParams = [':entity', ':context', ':token'];
private $clients = [];
private $log;
/**
* ServerHandler constructor.
*
* @throws \Exception
*/
public function __construct()
{
// create a log channel
$this->log = new Logger('ServerSocket');
$this->log->pushHandler(new StreamHandler('./tests/tests.log'));
}
public function onOpen(ConnectionContract $conn)
{
$this->clients[$conn->getUniqueSocketId()] = $conn;
$this->log->debug('Connection opend, total clients: ' . count($this->clients));
}
public function onMessage(ConnectionContract $recv, $msg)
{
$this->log->debug('Received message: ' . $msg);
$recv->send($msg);
}
public function onClose(ConnectionContract $conn)
{
unset($this->clients[$conn->getUniqueSocketId()]);
$this->log->debug('close: ' . print_r($this->clients, 1));
$conn->close();
}
/**
* @param ConnectionContract $conn
* @param WebSocketException $ex
*/
public function onError(ConnectionContract $conn, WebSocketException $ex)
{
echo 'Error occured: ' . $ex->printStack();
}
/**
* You may want to implement these methods to bring ping/pong events
*
* @param ConnectionContract $conn
* @param string $msg
*/
public function onPing(ConnectionContract $conn, $msg)
{
// TODO: Implement onPing() method.
}
/**
* @param ConnectionContract $conn
* @param $msg
* @return mixed
*/
public function onPong(ConnectionContract $conn, $msg)
{
// TODO: Implement onPong() method.
}
}
use WSSC\WebSocketServer;
use WSSCTEST\ServerHandler;
use WSSC\Components\ServerConfig;
$config = new ServerConfig();
$config->setClientsPerFork(2500);
$config->setStreamSelectTimeout(2 * 3600);
$webSocketServer = new WebSocketServer(new ServerHandler(), $config);
$webSocketServer->run();
use WSSC\WebSocketClient;
use \WSSC\Components\ClientConfig;
$client = new WebSocketClient('ws://localhost:8000/notifications/messanger/yourtoken123', new ClientConfig());
$client->send('{"user_id" : 123}');
echo $client->receive();
use WSSC\WebSocketClient;
use WSSC\Components\ClientConfig;
$config = new ClientConfig();
$config->setFragmentSize(8096);
$config->setTimeout(15);
$config->setHeaders([
'X-Custom-Header' => 'Foo Bar Baz',
]);
// if proxy settings is of need
$config->setProxy('127.0.0.1', '80');
$config->setProxyAuth('proxyUser', 'proxyPass');
$client = new WebSocketClient('ws://localhost:8000/notifications/messanger/yourtoken123', $config);
$conn->broadCast('hey everybody...');
// or to send multiple messages with 2 sec delay between them
$conn->broadCastMany(['Hello', 'how are you today?', 'have a nice day'], 2);
$config = new ServerConfig();
$config->setOrigins(["example.com", "otherexample.com"]);
$websocketServer = new WebSocketServer(new ServerHandler(), $config);
$websocketServer->run();
use WSSC\Components\ServerConfig;
use WSSC\WebSocketServer;
$config = new ServerConfig();
$config->setIsSsl(true)->setAllowSelfSigned(true)
->setCryptoType(STREAM_CRYPTO_METHOD_SSLv23_SERVER)
->setLocalCert("./tests/certs/cert.pem")->setLocalPk("./tests/certs/key.pem")
->setPort(8888);
$websocketServer = new WebSocketServer(new ServerHandler(), $config);
$websocketServer->run();
use WSSC\WebSocketServer;
use WSSCTEST\ServerHandler;
use WSSC\Components\ServerConfig;
$config = new ServerConfig();
// Set the read socket iteration delay in miliseconds
$config->setLoopingDelay(1);
$webSocketServer = new WebSocketServer(new ServerHandler(), $config);
$webSocketServer->run();