-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.php
More file actions
159 lines (133 loc) · 4.61 KB
/
backup.php
File metadata and controls
159 lines (133 loc) · 4.61 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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace Turted\Server;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Request;
use React\Http\Response;
use React\Http\Server;
use React\Stream\ThroughStream;
class TurtedServer
{
/**
* @var int
*/
private $port;
private $connections = [];
/**
* @var callable
*/
private $userResolver;
/** @var string */
private $baseUrl;
/**
* @var \React\EventLoop\LoopInterface
*/
private $loop;
public function __construct($config)
{
if (is_numeric($config)) {
$this->port = (int)$config;
} else {
if (isset($config['port'])) {
$this->port = (int)$config['port'];
}
}
if (isset($config['user_resolver']) {
if (is_callable($config['user_resolver'])) {
$this->userResolver = $config['user_resolver'];
}
}
if ($this->port <= 0) {
$this->port = 19195;
}
if (isset($config['base_url'])) {
$this->baseUrl = $config['base_url'];
}
}
private function handleRequest(ServerRequestInterface $request) {
$uri = $request->getUri();
var_dump($request->getCookieParams());
$path = str_replace($this->baseUrl, '', $uri->getPath());
if ($path === '') {
$path = '/';
}
var_dump($path);
}
public function start()
{
$this->loop = \React\EventLoop\Factory::create();
$port = '0.0.0.0:'.$this->port;
$http = new Server([$this,'handleRequest']);
function (ServerRequestInterface $request) use ($loop, &$connections, $baseUrl) {
if (($path === '/push') || ($path === '/push/')) {
// @TODO check "old style" to give info about version 4
if ($request->getMethod() === 'POST') {
$body = $request->getBody()->getContents();
$data = json_decode($body, true);
var_dump($data);
// $data ['time'] = date('H:i:s');
brodcast($data);
}
return new Response(200, ['Content-Type' => 'text/html',], 'ok');
}
if ($path === '/stream') {
$stream = new ThroughStream();
$stream->on(
'close',
function () use ($stream) {
remove($stream);
}
);
$id = $request->getHeaderLine('Last-Event-ID');
var_dump('Last ID: '.$id);
$connections[] = $stream;
$stream->write('as'.PHP_EOL);
$stream->write('retry: 8000'.PHP_EOL);
$headers = ['Cache-Control' => 'no-cache', 'Content-Type' => 'text/event-stream',];
return new Response(200, $headers, $stream);
}
return new Response(404, [], 'not found');
}
);
function brodcast($data)
{
global $connections;
global $id;
$json = json_encode($data);
/** @var ThroughStream[] $connections */
foreach ($connections as $connection) {
if ($connection->isWritable()) {
$id++;
$connection->write('event: ping'.PHP_EOL);
$connection->write('id: '.md5($id).PHP_EOL);
$connection->write('data: '.$json.PHP_EOL.PHP_EOL);
}
}
}
function remove($stream)
{
global $connections;
echo "Weg 1".PHP_EOL;
$pos = array_search($stream, $connections, true);
if ($pos !== false) {
unset($connections[$pos]);
}
}
$loop->addPeriodicTimer(
12,
function () use (&$connections) {
$memory = memory_get_usage() / 1024;
$formatted = number_format($memory, 3).'K';
echo "Current memory usage: {$formatted}\n";
echo count($connections)." connections\n";
$load = sys_getloadavg();
$data = ['time' => date('H:i:s'), 'load' => $load];
brodcast($data);
}
);
$socket = new \React\Socket\Server($port, $loop);
$http->listen($socket);
#echo 'Server now listening on http://localhost:'.$socket->getPort().PHP_EOL;
$loop->run();
brodcast("I'm running");
}
}