-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.php
More file actions
158 lines (132 loc) · 4.36 KB
/
server.php
File metadata and controls
158 lines (132 loc) · 4.36 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
use Workerman\Worker;
require_once './vendor/workerman/workerman/Autoloader.php';
(new PptSocketServer())->run();
class PptSocketServer
{
const CONNECTION_TYPE_PPT = 'connection_ppt';
const CONNECTION_TYPE_CONTROLLER = 'connection_controller';
protected $worker;
protected $globalUid = 0;
protected $globalPptConnection;
protected $globalControllerConnection;
public function __construct(int $port = 2346)
{
$this->initWorker($port);
}
public function run()
{
Worker::runAll();
}
protected function initWorker(int $port)
{
$this->worker = new Worker("websocket://0.0.0.0:" . $port);
$this->worker->count = 1;
$this->worker->onConnect = [$this, 'handleConnection'];
$this->worker->onMessage = [$this, 'handleMessage'];
$this->worker->onClose = [$this, 'handleClose'];
}
// 简单记录连接的 id 信息
public function handleConnection($connection)
{
$connection->uid = ++$this->globalUid;
}
public function handleMessage($connection, $data)
{
// 初始化 PPT 网页端的 connection
if ($this->setPptConnectionIfNull($connection, $data)) {
Log::info('ppt online');
return;
}
// 初始化 控制端页面的 connection
if ($this->setControllerConnectionIfNull($connection, $data)) {
Log::info('controller online');
return;
}
if (is_null($this->globalPptConnection)) {
Log::info('ppt offline; cant control');
return;
}
// 目前只允许一个控制器发送指令。
if (!is_null($this->globalControllerConnection)
&& $connection->uid !== $this->globalControllerConnection->uid
) {
Log::info('sorry, you are not correct controller ' . $connection->uid);
return;
}
// 转发控制端「指令」到 PPT 网页端
$this->globalPptConnection->send($data);
}
public function handleClose($connection)
{
// 判断并销毁 PPT 网页端或者控制端页面的 connection
$this->destructConnection($connection);
Log::info($connection->uid . ' offline by close websocket');
}
protected function destructConnection($connection)
{
if (isset($connection->type) && $connection->type === self::CONNECTION_TYPE_PPT) {
$this->globalPptConnection = null;
Log::info('ppt offline');
return true;
}
if (isset($connection->type) && $connection->type === self::CONNECTION_TYPE_CONTROLLER) {
$this->globalControllerConnection = null;
Log::info('controller offline');
return true;
}
return true;
}
/**
* 根据命令判断和初始化 PPT 网页端的 connection
*
* @param $connection
* @param $data
* @return bool
*/
protected function setPptConnectionIfNull($connection, $data)
{
if (!is_null($this->globalPptConnection)) return false;
if (!$this->requestConnectionIsPpt($data)) return false;
$connection->type = self::CONNECTION_TYPE_PPT;
$this->globalPptConnection = $connection;
return true;
}
/**
* 根据命令判断和初始化控制端页面的 connection
*
* @param $connection
* @param $data
* @return bool
*/
protected function setControllerConnectionIfNull($connection, $data)
{
if (!is_null($this->globalControllerConnection)) return false;
if (!$this->requestConnectionIsController($data)) return false;
$connection->type = self::CONNECTION_TYPE_CONTROLLER;
$this->globalControllerConnection = $connection;
return true;
}
public function requestConnectionIsPpt($data)
{
return $data === 'i am ppt';
}
public function requestConnectionIsController($data)
{
return $data === 'i am controller';
}
}
class Log
{
public static function info(...$logString)
{
$infos = func_get_args();
foreach ($infos as $key=> $info) {
file_put_contents(
"./debug.log",
"[ " . date('Y-m-d H:i:s') . " ] " . var_export($info, true) . PHP_EOL,
FILE_APPEND
);
}
}
}