-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
29 lines (25 loc) · 1.03 KB
/
server.php
File metadata and controls
29 lines (25 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
use Octamp\Server\Connection\Connection;
use Octamp\Server\Server;
use OpenSwoole\WebSocket\Frame;
require_once __DIR__ . '/../vendor/autoload.php';
$websocket = Server::createWebsocketServer('0.0.0.0', 8080, ['worker_num' => 2]);
$server = new Server($websocket);
$server->on('beforeStart', function (Server $server) {
$adapter = new \Octamp\Server\Adapter\RedisAdapter('0.0.0.0', 6379);
$server->setAdapter($adapter);
$server->setGenerator(new \Octamp\Server\Generator\RedisIDGenerator($server, $adapter));
});
$server->on('open', function (Server $server, Connection $connection) {
$connection->send('Welcome');
});
$server->on('message', function (Server $server, Connection $connection, Frame $frame) {
$connections = $server->getConnectionStorage()->getAll();
foreach ($connections as $connection) {
$connection->send($frame->data);
}
});
$server->on('close', function (Server $server, Connection $connection) {
echo 'Connection Closed: ' . $connection->getId() . PHP_EOL;
});
$server->start();