1. Go to this page and download the library: Download mita/uranus-socket-server 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/ */
mita / uranus-socket-server example snippets
namespace Mita\UranusSocketServer\Examples\Chat\Controllers;
use Mita\UranusSocketServer\Controllers\BaseController;
use Mita\UranusSocketServer\Managers\ConnectionManager;
use Mita\UranusSocketServer\Packets\PacketInterface;
use Ratchet\ConnectionInterface;
class ChatController extends BaseController
{
public function handle(ConnectionInterface $conn, PacketInterface $packet, array $params)
{
if ($params['_route'] === 'join_room') {
$conn->send("You joined room " . $params['roomId']);
} elseif ($params['_route'] === 'room_publish') {
$conn->send("Message to room " . $params['roomId'] . ": " . $packet->getMessage());
}
}
}
public function registerEventListener(string $eventName, callable $listener)
public function handle(ConnectionInterface $conn, PacketInterface $packet, callable $next)
public function add(MiddlewareInterface $middleware)
public function process(ConnectionInterface $conn, PacketInterface $packet, callable $finalHandler)
public function add(ConnectionInterface $conn)
public function remove(ConnectionInterface $conn)
public function addListener(string $eventName, callable $listener)
public function dispatch(string $eventName, $eventData = null)
public function getRoute(): string
public function getMessage()
public function getMetadata(string $key = null)
public function createFromJson(string $json): PacketInterface
public function addPlugin(PluginInterface $plugin)
public function registerPlugins(EventDispatcherInterface $dispatcher)
public function bootPlugins()
use Mita\UranusSocketServer\Events\EventDispatcherInterface;
use Mita\UranusSocketServer\Plugins\PluginInterface;
class MyCustomPlugin implements PluginInterface
{
public function register(EventDispatcherInterface $dispatcher)
{
// Register events or middleware here
}
public function boot()
{
// Code to run when the plugin is booted
}
public function unregister(EventDispatcherInterface $dispatcher)
{
// Unregister events or middleware here
}
}
$myPlugin = new MyCustomPlugin();
$socketServer->addPlugin($myPlugin);
$socketServer->registerEventListener('connection.opened', function($conn) {
echo "New connection opened with ID: " . $conn->resourceId . "\n";
});
$socketServer->registerEventListener('message.received', function($data) {
$conn = $data['connection'];
$msg = $data['message'];
echo "Message received from connection {$conn->resourceId}: {$msg}\n";
});