PHP code example of cydrickn / swoole-websocket-bundle
1. Go to this page and download the library: Download cydrickn/swoole-websocket-bundle 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/ */
cydrickn / swoole-websocket-bundle example snippets
$server->on('connection', function (\Cydrickn\SocketIO\Socket $socket) {
$socket->emit('hello', 'world');
});
$server->on('chat', function (\Cydrickn\SocketIO\Socket $socket, $message) {
$socket->broadcast()->emit('chat', $message);
});
namespace App\Controller;
use Cydrickn\SwooleWebsocketBundle\Attribute\RouteAttribute;
class SocketController
{
#[RouteAttribute(path: 'connection')]
public function connection(\Cydrickn\SocketIO\Socket $socket)
{
$socket->emit('hello', 'world');
}
#[RouteAttribute(path: 'chat')]
public function anotherMethod(\Cydrickn\SocketIO\Socket $socket, $message)
{
$socket->broadcast()->emit('chat', $message);
}
}