forked from swoole-inc/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
157 lines (141 loc) · 4.87 KB
/
server.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
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
<?php
use Swoole\Http\Request;
use Swoole\Http\Response;
const WEBROOT = __DIR__ . '/web';
$subject_connnection_map = array();
error_reporting(E_ALL);
\Swoole\Coroutine\run(
function () {
$server = new Swoole\Coroutine\Http\Server('0.0.0.0', 9509, false);
// $server = new Swoole\Coroutine\Http\Server('0.0.0.0', 9509, true);
// $server->set(
// [
// 'ssl_key_file' => __DIR__ . '/config/ssl.key',
// 'ssl_cert_file' => __DIR__ . '/config/ssl.crt',
// ]
// );
$server->handle(
'/',
function (Request $req, Response $resp) {
//websocket
if (isset($req->header['upgrade']) and $req->header['upgrade'] == 'websocket') {
$resp->upgrade();
$resp->subjects = array();
while (true) {
$frame = $resp->recv();
if (empty($frame)) {
break;
}
$data = json_decode($frame->data, true);
var_dump($data);
switch ($data['cmd']) {
// 订阅主题
case 'subscribe':
$subject = $data['subject'];
subscribe($subject, $resp);
break;
// 向某个主题发布消息
case 'publish':
$subject = $data['subject'];
$event = $data['event'];
$data = $data['data'];
publish($subject, $event, $data, $resp);
break;
}
}
destry_connection($resp);
return;
}
//http
$path = $req->server['request_uri'];
if ($path == '/') {
$resp->end(exec_php_file(WEBROOT . '/index.html'));
} else {
$file = realpath(WEBROOT . $path);
if (false === $file) {
$resp->status(404);
$resp->end('<h3>404 Not Found</h3>');
return;
}
// Security check! Very important!!!
if (strpos($file, WEBROOT) !== 0) {
$resp->status(400);
return;
}
if (\pathinfo($file, PATHINFO_EXTENSION) === 'php') {
$resp->end(exec_php_file($file));
return;
}
if (isset($req->header['if-modified-since']) and !empty($if_modified_since = $req->header['if-modified-since'])) {
// Check 304.
$info = \stat($file);
$modified_time = $info ? \date(
'D, d M Y H:i:s',
$info['mtime']
) . ' ' . \date_default_timezone_get() : '';
if ($modified_time === $if_modified_since) {
$resp->status(304);
$resp->end();
return;
}
}
$resp->sendfile($file);
}
}
);
$server->start();
}
);
// 订阅
function subscribe($subject, $connection)
{
global $subject_connnection_map;
$connection->subjects[$subject] = $subject;
$subject_connnection_map[$subject][$connection->fd] = $connection;
}
// 取消订阅
function unsubscribe($subject, $connection)
{
global $subject_connnection_map;
unset($subject_connnection_map[$subject][$connection->fd]);
}
// 向某个主题发布事件
function publish($subject, $event, $data, $exclude)
{
global $subject_connnection_map;
if (empty($subject_connnection_map[$subject])) {
return;
}
foreach ($subject_connnection_map[$subject] as $connection) {
if ($exclude == $connection) {
continue;
}
$connection->push(
json_encode(
array(
'cmd' => 'publish',
'event' => $event,
'data' => $data
)
)
);
}
}
// 清理主题映射数组
function destry_connection($connection)
{
foreach ($connection->subjects as $subject) {
unsubscribe($subject, $connection);
}
}
function exec_php_file($file)
{
\ob_start();
// Try to include php file.
try {
include $file;
} catch (\Exception $e) {
echo $e;
}
return \ob_get_clean();
}