-
Notifications
You must be signed in to change notification settings - Fork 37
/
WebSocketServer.php
226 lines (200 loc) · 6.2 KB
/
WebSocketServer.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
/**
* @link https://github.com/consik/yii2-websocket
* @category yii2-extension
* @package consik\yii2websocket
*
* @author Sergey Poltaranin <consigliere.kz@gmail.com>
* @copyright Copyright (c) 2016
*/
namespace consik\yii2websocket;
use consik\yii2websocket\events\ExceptionEvent;
use consik\yii2websocket\events\WSClientCommandEvent;
use consik\yii2websocket\events\WSClientErrorEvent;
use consik\yii2websocket\events\WSClientEvent;
use consik\yii2websocket\events\WSClientMessageEvent;
use Ratchet\ConnectionInterface;
use Ratchet\Http\HttpServer;
use Ratchet\MessageComponentInterface;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use yii\base\Component;
class WebSocketServer extends Component implements MessageComponentInterface
{
/**
* @event yii\base\Event Triggered when binding is successfully completed
*/
const EVENT_WEBSOCKET_OPEN = 'ws_open';
/**
* @event yii\base\Event Triggered when socket listening is closed
*/
const EVENT_WEBSOCKET_CLOSE = 'ws_close';
/**
* @event ExceptionEvent Triggered when throwed Exception on binding socket
*/
const EVENT_WEBSOCKET_OPEN_ERROR = 'ws_open_error';
/**
* @event WSClientEvent Triggered when client connected to the server
*/
const EVENT_CLIENT_CONNECTED = 'ws_client_connected';
/**
* @event WSClientErrorEvent Triggered when an error occurs on a Connection
*/
const EVENT_CLIENT_ERROR = 'ws_client_error';
/**
* @event WSClientEvent Triggered when client close connection with server
*/
const EVENT_CLIENT_DISCONNECTED = 'ws_client_disconnected';
/**
* @event WSClientMessageEvent Triggered when message recieved from client
*/
const EVENT_CLIENT_MESSAGE = 'ws_client_message';
/**
* @event WSClientCommandEvent Triggered when controller starts user's command
*/
const EVENT_CLIENT_RUN_COMMAND = 'ws_client_run_command';
/**
* @event WSClientCommandEvent Triggered when controller finished user's command
*/
const EVENT_CLIENT_END_COMMAND = 'ws_client_end_command';
/**
* @var int $port
*/
public $port = 8080;
/**
* @var bool $closeConnectionOnError
*/
protected $closeConnectionOnError = true;
/**
* @var bool $runMessageCommands
*/
protected $runClientCommands = true;
/**
* @var IoServer|null $server
*/
protected $server = null;
/**
* @var null|\SplObjectStorage $clients
*/
protected $clients = null;
/**
* @return bool
*
* @event yii\base\Event EVENT_WEBSOCKET_OPEN
* @event ExceptionEvent EVENT_WEBSOCKET_OPEN_ERROR
*/
public function start()
{
try {
$this->server = IoServer::factory(
new HttpServer(
new WsServer(
$this
)
),
$this->port
);
$this->trigger(self::EVENT_WEBSOCKET_OPEN);
$this->clients = new \SplObjectStorage();
$this->server->run();
return true;
} catch (\Exception $e) {
$errorEvent = new ExceptionEvent([
'exception' => $e
]);
$this->trigger(self::EVENT_WEBSOCKET_OPEN_ERROR, $errorEvent);
return false;
}
}
/**
* @return void
*
* @event yii\base\Event EVENT_WEBSOCKET_CLOSE
*/
public function stop()
{
$this->server->socket->shutdown();
$this->trigger(self::EVENT_WEBSOCKET_CLOSE);
}
/**
* @param ConnectionInterface $conn
*
* @event WSClientEvent EVENT_CLIENT_CONNECTED
*/
function onOpen(ConnectionInterface $conn)
{
$this->trigger(self::EVENT_CLIENT_CONNECTED, new WSClientEvent([
'client' => $conn
]));
$this->clients->attach($conn);
}
/**
* @param ConnectionInterface $conn
*
* @event WSClientEvent EVENT_CLIENT_DISCONNECTED
*/
function onClose(ConnectionInterface $conn)
{
$this->trigger(self::EVENT_CLIENT_DISCONNECTED, new WSClientEvent([
'client' => $conn
]));
$this->clients->detach($conn);
}
/**
* @param ConnectionInterface $conn
* @param \Exception $e
*
* @event WSClientErrorEvent EVENT_CLIENT_ERROR
*/
function onError(ConnectionInterface $conn, \Exception $e)
{
$this->trigger(self::EVENT_CLIENT_ERROR, new WSClientErrorEvent([
'client' => $conn,
'exception' => $e
]));
if ($this->closeConnectionOnError) {
$conn->close();
}
}
/**
* @param ConnectionInterface $from
* @param string $msg
*
* @event WSClientMessageEvent EVENT_CLIENT_MESSAGE
* @event WSClientCommandEvent EVENT_CLIENT_RUN_COMMAND
* @event WSClientCommandEvent EVENT_CLIENT_END_COMMAND
*/
function onMessage(ConnectionInterface $from, $msg)
{
$this->trigger(self::EVENT_CLIENT_MESSAGE, new WSClientMessageEvent([
'client' => $from,
'message' => $msg
]));
if ($this->runClientCommands) {
$command = $this->getCommand($from, $msg);
if ($command && method_exists($this, 'command' . ucfirst($command))) {
$this->trigger(self::EVENT_CLIENT_RUN_COMMAND, new WSClientCommandEvent([
'client' => $from,
'command' => $command
]));
$result = call_user_func([$this, 'command' . ucfirst($command)], $from, $msg);
$this->trigger(self::EVENT_CLIENT_END_COMMAND, new WSClientCommandEvent([
'client' => $from,
'command' => $command,
'result' => $result
]));
}
}
}
/**
* @param ConnectionInterface $from
* @param $msg
* @return null|string - _NAME_ of command that implemented in class method command_NAME_()
*/
protected function getCommand(ConnectionInterface $from, $msg)
{
return null;
}
}