<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Connection\AsyncTcpConnection;
// SSL context.
$context = [ 'ssl' => [ 'local_cert' => '/etc/letsencrypt/live/utelimited.ru/fullchain.pem',
'local_pk' => '/etc/letsencrypt/live/utelimited.ru/privkey.pem',
'verify_peer' => false,
]
];
global $connection;
//==============================server
// Create a Websocket server
$ws_worker = new Worker ("websocket://0.0.0.0:100", $context);
$ws_worker->transport = 'ssl';
// Emitted when new connection come
$ws_worker-> onConnect = function($connection)
{
$connection->onWebSocketConnect = function($connection)
{
echo "New connection\n";
$connection->send ('Server OK: ' . $data);
};
};
// Emitted when data received
$ws_worker-> onMessage = function($connection, $data)
{
// Send hello $data
$connection->send ('hello ' . $data);
};
// Emitted when connection closed
$ws_worker-> onClose = function($connection)
{
echo "Connection closed\n";
};
//=============================== CLIENT
$worker_wss = new Worker ();
$worker_wss ->onWorkerStart = function() {
foreach($ws_worker->connection as $connection) {
$connection->send("connect client");
}
// Websocket protocol for client.
$ws_connection = new AsyncTcpConnection('ws://ws.eodhistoricaldata.com/ws/crypto?api_token=demo');
$ws_connection-> onConnect = function ($connection_wss) {
foreach($ws_worker->connections as $connection) {
$connection->send("connect client");
}
};
$ws_connection->onMessage = function ($connection_wss, $data) {
foreach($ws_worker->connections as $connection) {
$connection->send($data);
}
};
$ws_connection->onError = function ($connection_wss, $code, $msg) {
echo "Error: $msg\n";
foreach($ws_worker->connections as $connection) {
$connection->send($msg);
}
};
$ws_connection->onClose = function ($connection_wss) {
echo "Connection closed\n";
foreach($ws_worker->connections as $connection) {
$connection->send("close client");
}
};
$ws_connection->connect();
};
// Run worker
Worker::runAll();
foreach($ws_worker->connection as $connection) {
$connection->send("connect client");
}
$ws_connection = new AsyncTcpConnection('ws://ws.eodhistoricaldata.com/ws/crypto?api_token=demo');