forked from Cortana666/yz_facetime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex1.php
226 lines (185 loc) · 8.38 KB
/
index1.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
use Workerman\Worker;
use Workerman\Lib\Timer;
Use Services\Base;
Use Services\Worker as WorkerService;
require_once __DIR__ . '/vendor/autoload.php';
define('HEARTBEAT_TIME', 60);
$ws_worker = new Worker("websocket://0.0.0.0:2000");
$ws_worker->count = 1;
$ws_worker->room = array();
$worker->onConnect = function ($connection) {
// 30秒内未发送token断开连接
$connection->auth_timer_id = Timer::add(30, function()use($connection){
$connection->close(Base::success("cLogout", "未接收到Token"));
}, null, false);
};
$ws_worker->onWorkerStart = function ($ws_worker) {
// 初始化Database操作
global $db;
$db = new \Workerman\MySQL\Connection('82.156.126.93', '3306', 'remote', 'Qwer1234;', 'yz_kaowu');
// 心跳检测
Timer::add(10, function()use($ws_worker){
$time_now = time();
foreach($ws_worker->connections as $connection) {
$connection->send(Base::success("pong"));
if ($time_now - $connection->lastMessageTime > HEARTBEAT_TIME) {
$connection->close(Base::error("cLogout", "系统检测到您在考场中出现问题,请重新进入考场"));
}
}
});
};
$ws_worker->onMessage = function ($connection, $data) {
global $ws_worker;
global $db;
// 更新心跳时间
$connection->lastMessageTime = time();
// 解析json
$data = json_decode($data, true);
switch ($data['send_type']) {
case 'sLogin':
Timer::del($connection->auth_timer_id);
if (!Base::check_token($connection, $data)) {
$connection->close(Base::error("cLogout", "Token验证失败,请重新进入考场"));
} else {
WorkerService::set_connection($ws_worker, $db, $connection);
}
break;
case 'sLogin':
Timer::del($connection->auth_timer_id);
Base::check_token($data);
break;
default:
# code...
break;
}
// WorkerService::$data['send_type']($ws_worker, $db, $connection, $data);
// if ($data['send_type'] == "sLogin") {
// // 验证学生端/教师端token
// if (!$face_token = Base::check_user_token($db, $data)) {
// // token验证失败
// $connection->close(Base::error("login", "签名验证失败"));
// } else {
// // 建立连接
// WorkerService::build_link($ws_worker, $connection, $data);
// // token验证成功返回面试token
// $connection->send(Base::success("login", "建立连接成功", '', ['face_token'=>$face_token]));
// if ($data['type'] == 1) {
// // 发送学生列表
// WorkerService::send_student_list($ws_worker, $db, $connection);
// // 发送学生状态
// WorkerService::send_student_status($ws_worker, $connection);
// }
// if ($data['type'] == 2) {
// // 广播列表
// WorkerService::send_student_wait($ws_worker, $db, $connection);
// // 广播状态
// WorkerService::send_student_status($ws_worker, $connection, true);
// }
// }
// }
// if ($data['send_type'] == "login") {
// // 验证学生端/教师端token
// if (!$face_token = Base::check_user_token($db, $data)) {
// // token验证失败
// $connection->close(Base::error("login", "签名验证失败"));
// } else {
// // 建立连接
// WorkerService::build_link($ws_worker, $connection, $data);
// // token验证成功返回面试token
// $connection->send(Base::success("login", "建立连接成功", '', ['face_token'=>$face_token]));
// if ($data['type'] == 1) {
// // 发送学生列表
// WorkerService::send_student_list($ws_worker, $db, $connection);
// // 发送学生状态
// WorkerService::send_student_status($ws_worker, $connection);
// }
// if ($data['type'] == 2) {
// // 广播列表
// WorkerService::send_student_wait($ws_worker, $db, $connection);
// // 广播状态
// WorkerService::send_student_status($ws_worker, $connection, true);
// }
// }
// }
// if ($data['send_type'] == "invite") {
// // 验证面试token
// if (!Base::check_face_token($connection, $data['face_token'])) {
// // token验证失败
// $connection->close(Base::error("login", "签名验证失败"));
// unset($ws_worker->channel[$connection->channel][1][$connection->user_id]);
// } else {
// // token验证成功开始面试
// if ($data['user_id']) {
// $ws_worker->channel[$connection->channel][2][$data['user_id']]['connection']->send(Base::success("invite", "开始面试"));
// } else {
// // foreach ($variable as $key => $value) {
// // # code...
// // }
// }
// }
// }
// if ($data['send_type'] == "doinvite") {
// // 验证面试token
// if (!Base::check_face_token($connection, $data['face_token'])) {
// // token验证失败
// $connection->close(Base::error("login", "签名验证失败"));
// unset($ws_worker->channel[$connection->channel][1][$connection->user_id]);
// } else {
// // token验证成功开始面试
// $ws_worker->channel[$connection->channel][2][$connection->user_id]['status'] = 2;
// // 广播列表
// WorkerService::send_student_wait($ws_worker, $db, $connection);
// // 广播状态
// WorkerService::send_student_status($ws_worker, $connection, true);
// }
// }
// if ($data['send_type'] == "close") {
// // 验证面试token
// if (!Base::check_face_token($connection, $data['face_token'])) {
// // token验证失败
// $connection->close(Base::error("login", "签名验证失败"));
// unset($ws_worker->channel[$connection->channel][1][$connection->user_id]);
// } else {
// // token验证成功开始面试
// $connection->close(Base::error("login", "未接受邀请,请重新进入房间!"));
// }
// }
// if ($data['send_type'] == "kick") {
// // 验证面试token
// if (!Base::check_face_token($connection, $data['face_token'])) {
// // token验证失败
// $connection->close(Base::error("login", "签名验证失败"));
// unset($ws_worker->channel[$connection->channel][1][$connection->user_id]);
// } else {
// // token验证成功执行踢出操作
// $ws_worker->channel[$connection->channel][2][$data['user_id']]->close(Base::success("kick", "教师踢出操作"));
// unset($ws_worker->channel[$connection->channel][2][$data['user_id']]);
// }
// // 广播列表
// WorkerService::send_list($ws_worker, $db, $connection);
// }
// if ($data['send_type'] == "end") {
// // 验证面试token
// if (!Base::check_face_token($connection, $data['face_token'])) {
// // token验证失败
// $connection->close(Base::error("login", "签名验证失败"));
// unset($ws_worker->channel[$connection->channel][1][$connection->user_id]);
// } else {
// // token验证成功开始面试
// $ws_worker->channel[$connection->channel][2][$data['user_id']]->send(Base::success("end", "结束面试"));
// }
// // 广播列表
// WorkerService::send_list($ws_worker, $db, $connection);
// }
};
$ws_worker->onClose = function ($connection) {
// global $ws_worker;
// if ($connection->type == 2) {
// // 广播状态
// $ws_worker->channel[$connection->channel][2][$connection->user_id]['leave_status'] = $ws_worker->channel[$connection->channel][2][$connection->user_id]['status'];
// $ws_worker->channel[$connection->channel][2][$connection->user_id]['status'] = 0;
// WorkerService::send_student_status($ws_worker, $connection, true);
// }
};
Worker::runAll();