1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is a part of Woketo package. |
5
|
|
|
* |
6
|
|
|
* (c) Nekland <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full license, take a look to the LICENSE file |
9
|
|
|
* on the root directory of this project |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Nekland\Woketo\Server; |
13
|
|
|
|
14
|
|
|
use Nekland\Woketo\Exception\RuntimeException; |
15
|
|
|
use Nekland\Woketo\Http\Request; |
16
|
|
|
use Nekland\Woketo\Message\MessageHandlerInterface; |
17
|
|
|
use Nekland\Woketo\Rfc6455\FrameFactory; |
18
|
|
|
use Nekland\Woketo\Rfc6455\Message; |
19
|
|
|
use Nekland\Woketo\Rfc6455\MessageFactory; |
20
|
|
|
use Nekland\Woketo\Rfc6455\MessageHandler\CloseFrameHandler; |
21
|
|
|
use Nekland\Woketo\Rfc6455\MessageHandler\RsvCheckFrameHandler; |
22
|
|
|
use Nekland\Woketo\Rfc6455\MessageHandler\WrongOpcodeHandler; |
23
|
|
|
use Nekland\Woketo\Rfc6455\MessageHandler\PingFrameHandler; |
24
|
|
|
use Nekland\Woketo\Rfc6455\MessageProcessor; |
25
|
|
|
use Nekland\Woketo\Rfc6455\ServerHandshake; |
26
|
|
|
use React\EventLoop\LoopInterface; |
27
|
|
|
use React\Socket\ConnectionInterface; |
28
|
|
|
|
29
|
|
|
class Websocket |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var resource Socket of the server |
33
|
|
|
*/ |
34
|
|
|
private $socket; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int Store the port for debug purpose. |
38
|
|
|
*/ |
39
|
|
|
private $port; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $address; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Request |
48
|
|
|
*/ |
49
|
|
|
private $request; |
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ServerHandshake |
53
|
|
|
*/ |
54
|
|
|
private $handshake; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var MessageHandlerInterface |
58
|
|
|
*/ |
59
|
|
|
private $messageHandler; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* tmp var for test purpose |
63
|
|
|
* @var Message |
64
|
|
|
*/ |
65
|
|
|
private $message; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
private $connections; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var LoopInterface |
74
|
|
|
*/ |
75
|
|
|
private $loop; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var MessageProcessor |
79
|
|
|
*/ |
80
|
|
|
private $messageProcessor; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
private $config; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Websocket constructor. |
89
|
|
|
* |
90
|
|
|
* @param int $port The number of the port to bind |
91
|
|
|
* @param string $address The address to listen on (by default 127.0.0.1) |
92
|
|
|
* @param array $config |
93
|
|
|
*/ |
94
|
|
|
public function __construct($port, $address = '127.0.0.1', $config = []) |
95
|
|
|
{ |
96
|
|
|
$this->address = $address; |
97
|
|
|
$this->port = $port; |
98
|
|
|
$this->handshake = new ServerHandshake(); |
99
|
|
|
$this->connections = []; |
100
|
|
|
$this->setConfig($config); |
101
|
|
|
$this->buildMessageProcessor(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setMessageHandler($messageHandler) |
105
|
|
|
{ |
106
|
|
|
if (!$messageHandler instanceof MessageHandlerInterface && !is_string($messageHandler)) { |
107
|
|
|
throw new \InvalidArgumentException('The message handler must be an instance of MessageHandlerInterface or a string.'); |
108
|
|
|
} |
109
|
|
|
if (is_string($messageHandler)) { |
110
|
|
|
try { |
111
|
|
|
$reflection = new \ReflectionClass($messageHandler); |
112
|
|
|
if(!$reflection->implementsInterface('Nekland\Woketo\Message\MessageHandlerInterface')) { |
113
|
|
|
throw new \InvalidArgumentException('The messageHandler must implement MessageHandlerInterface'); |
114
|
|
|
} |
115
|
|
|
} catch (\ReflectionException $e) { |
116
|
|
|
throw new \InvalidArgumentException('The messageHandler must be a string representing a class.'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
$this->messageHandler = $messageHandler; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function start() |
123
|
|
|
{ |
124
|
|
|
$this->message = new Message(); |
125
|
|
|
$this->loop = \React\EventLoop\Factory::create(); |
126
|
|
|
|
127
|
|
|
$socket = new \React\Socket\Server($this->loop); |
128
|
|
|
$socket->on('connection', function ($socketStream) { |
129
|
|
|
$this->onNewConnection($socketStream); |
130
|
|
|
}); |
131
|
|
|
$socket->listen($this->port); |
132
|
|
|
|
133
|
|
|
$this->loop->run(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param ConnectionInterface $socketStream |
138
|
|
|
*/ |
139
|
|
|
private function onNewConnection(ConnectionInterface $socketStream) |
140
|
|
|
{ |
141
|
|
|
$messageHandler = $this->messageHandler; |
142
|
|
|
if (is_string($messageHandler)) { |
143
|
|
|
$messageHandler = new $messageHandler; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->connections[] = new Connection($socketStream, $messageHandler, $this->loop, $this->messageProcessor); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Build the message processor with configuration |
151
|
|
|
*/ |
152
|
|
|
private function buildMessageProcessor() |
153
|
|
|
{ |
154
|
|
|
$this->messageProcessor = new MessageProcessor( |
155
|
|
|
new FrameFactory($this->config['frame']), |
156
|
|
|
new MessageFactory($this->config['message']) |
157
|
|
|
); |
158
|
|
|
$this->messageProcessor->addHandler(new PingFrameHandler()); |
159
|
|
|
$this->messageProcessor->addHandler(new CloseFrameHandler()); |
160
|
|
|
$this->messageProcessor->addHandler(new WrongOpcodeHandler()); |
161
|
|
|
$this->messageProcessor->addHandler(new RsvCheckFrameHandler()); |
162
|
|
|
|
163
|
|
|
foreach ($this->config['messageHandlers'] as $handler) { |
164
|
|
|
if (!$handler instanceof MessageHandlerInterface) { |
165
|
|
|
throw new RuntimeException(sprintf('%s is not an instance of MessageHandlerInterface but must be !', get_class($handler))); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Sets the configuration |
172
|
|
|
* |
173
|
|
|
* @param array $config |
174
|
|
|
*/ |
175
|
|
|
private function setConfig(array $config) |
176
|
|
|
{ |
177
|
|
|
$this->config = array_merge([ |
178
|
|
|
'frame' => [], |
179
|
|
|
'message' => [], |
180
|
|
|
'messageHandlers' => [] |
181
|
|
|
], $config); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.