-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.php
64 lines (48 loc) · 1.73 KB
/
feed.php
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
use DevPledge\WebSocket\Connection;
use DevPledge\WebSocket\Connections;
use Predis\Client;
use Predis\Collection\Iterator\HashKey;
use Predis\Collection\Iterator\Keyspace;
date_default_timezone_set('UTC');
require __DIR__ . '/vendor/autoload.php';
$cache = new Client( [
'scheme' => 'tcp',
'host' => 'cache',
'port' => 6379,
] );
$websocket = new swoole_websocket_server( getenv( 'API_DOMAIN' ), getenv( 'SWOOLE_PORT' ) );
$connections = new Connections( $websocket, $cache );
$websocket->on( 'open', function ( swoole_websocket_server $server, $request ) use ( & $connections ) {
$connections->setWebSocketServer( $server );
echo 'open REQUEST:' . PHP_EOL;
var_dump( $request );
try {
$connection = new Connection( $request, $connections );
$connection->push( (object) [ 'connection_id' => $connection->getConnectionId() ] );
} catch ( TypeError | Exception $exception ) {
echo 'error - ' . $exception->getMessage() . PHP_EOL . $exception->getTraceAsString() . PHP_EOL;
}
} );
$websocket->on( 'message', function ( swoole_websocket_server $server, $frame ) use ( & $connections ) {
if ( $frame->data == '.' ) {
echo '.';
} else {
$connections->setWebSocketServer( $server );
echo 'MESSAGE' . PHP_EOL;
try {
var_dump( $frame );
echo PHP_EOL;
$connections->processFrameIntoConnection( $frame );
} catch ( TypeError | Exception $exception ) {
echo 'error - ' . $exception->getMessage() . PHP_EOL . $exception->getTraceAsString() . PHP_EOL;
}
}
} );
$websocket->on( 'close', function ( swoole_websocket_server $server, $fd ) use ( & $connections ) {
$connections->setWebSocketServer( $server );
echo 'remove ' . $fd . PHP_EOL;
$connections->removeConnection( $fd );
echo PHP_EOL;
} );
$websocket->start();