-
Notifications
You must be signed in to change notification settings - Fork 133
/
websocket-events.php
58 lines (46 loc) · 1.5 KB
/
websocket-events.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
<?php
/**
* Get all updates from MadelineProto EventHandler running inside TelegramApiServer via websocket.
* @see \TelegramApiServer\Controllers\EventsController
*/
use Amp\Websocket\Client\WebsocketHandshake;
use Revolt\EventLoop;
use function Amp\async;
use function Amp\delay;
use function Amp\Websocket\Client\connect;
require 'vendor/autoload.php';
$shortopts = 'u::';
$longopts = [
'url::',
];
$options = getopt($shortopts, $longopts);
$options = [
'url' => $options['url'] ?? $options['u'] ?? 'ws://127.0.0.1:9503/events',
];
echo "Connecting to: {$options['url']}" . PHP_EOL;
async(function () use ($options) {
while (true) {
try {
$handshake = (new WebsocketHandshake($options['url']));
$connection = connect($handshake);
$connection->onClose(static function () use ($connection) {
if ($connection->isClosed()) {
printf("Connection closed. Reason: %s\n", $connection->getCloseInfo()->getReason());
}
});
echo 'Waiting for events...' . PHP_EOL;
while ($message = $connection->receive()) {
$payload = $message->buffer();
printf("[%s] Received event: %s\n", date('Y-m-d H:i:s'), $payload);
}
} catch (Throwable $e) {
printf("Error: %s\n", $e->getMessage());
}
delay(0.1);
}
});
if (defined('SIGINT')) {
$signal = Amp\trapSignal([SIGINT, SIGTERM]);
} else {
EventLoop::run();
}