-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.php
More file actions
30 lines (24 loc) · 763 Bytes
/
server.php
File metadata and controls
30 lines (24 loc) · 763 Bytes
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
30
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
// Create a Websocket server
$ws_worker = new Worker("websocket://127.0.0.1:2346");
// 4 processes
$ws_worker->count = 4;
// Emitted when new connection come
$ws_worker->onConnect = function ($connection) {
echo "New connection\n";
};
// Emitted when data received
$ws_worker->onMessage = function ($connection, $data) {
// Simulate data from database
$connection->send('{"Name": "Crime and Punishment", "Author": "Dostoevsky"}');
sleep(5);
$connection->send('{"Name": "Woe from Wit", "Author": "Griboedov"}');
};
// Emitted when connection closed
$ws_worker->onClose = function ($connection) {
echo "Connection closed\n";
};
// Run worker
Worker::runAll();