/
HandshakeListener.php
172 lines (143 loc) 路 5.57 KB
/
HandshakeListener.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
<?php declare(strict_types=1);
namespace Swoft\WebSocket\Server\Swoole;
use Swoft;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\BeanFactory;
use Swoft\Co;
use Swoft\Context\Context;
use Swoft\Http\Message\Request as Psr7Request;
use Swoft\Http\Message\Response as Psr7Response;
use Swoft\Server\Contract\HandshakeInterface;
use Swoft\Session\Session;
use Swoft\SwoftEvent;
use Swoft\WebSocket\Server\Connection;
use Swoft\WebSocket\Server\Context\WsHandshakeContext;
use Swoft\WebSocket\Server\Contract\WsModuleInterface;
use Swoft\WebSocket\Server\Helper\WsHelper;
use Swoft\WebSocket\Server\WsDispatcher;
use Swoft\WebSocket\Server\WsErrorDispatcher;
use Swoft\WebSocket\Server\WsServerEvent;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Throwable;
use function server;
/**
* Class HandshakeListener
*
* @since 2.0
*
* @Bean()
*/
class HandshakeListener implements HandshakeInterface
{
/**
* Ws Handshake event
*
* @param Request $request
* @param Response $response
*
* @return bool
* @throws Throwable
*/
public function onHandshake(Request $request, Response $response): bool
{
$fd = $request->fd;
$sid = (string)$fd;
server()->log("Handshake: conn#$fd start an websocket connection request", [], 'debug');
// If sec-websocket-key error
$status = false;
$secKey = $request->header['sec-websocket-key'];
if (WsHelper::isInvalidSecKey($secKey)) {
server()->log("Handshake: shake hands failed with the #$fd. 'sec-websocket-key' is error!");
$response->end('sec-websocket-key is invalid!');
return false;
}
// Initialize psr7 Request and Response
$psr7Req = Psr7Request::new($request);
$psr7Res = Psr7Response::new($response);
// Initialize connection session and context
$ctx = WsHandshakeContext::new($psr7Req, $psr7Res);
$conn = Connection::new($fd, $psr7Req, $psr7Res);
// Bind connection and bind cid => sid(fd)
Session::set($sid, $conn);
// Storage context
Context::set($ctx);
try {
Swoft::trigger(WsServerEvent::HANDSHAKE_BEFORE, $fd, $request, $response);
/** @var WsDispatcher $dispatcher */
$dispatcher = BeanFactory::getSingleton('wsDispatcher');
/** @var Psr7Response $psr7Res */
[$status, $psr7Res] = $dispatcher->handshake($psr7Req, $psr7Res);
if (true !== $status) {
server()->log("Handshake: conn#$fd handshake check failed");
$psr7Res->quickSend();
// NOTICE: Rejecting a handshake still triggers a close event.
return false;
}
// Config response
$psr7Res = $psr7Res->withStatus(101)->withHeaders(WsHelper::handshakeHeaders($secKey));
if ($wsProtocol = $request->header['sec-websocket-protocol'] ?? '') {
$psr7Res = $psr7Res->withHeader('Sec-WebSocket-Protocol', $wsProtocol);
}
// Response handshake successfully
$meta = $conn->getMetadata();
$conn->setHandshake(true);
$psr7Res->quickSend();
server()->log("Handshake: conn#{$fd} handshake successful! meta:", $meta, 'debug');
Swoft::trigger(WsServerEvent::HANDSHAKE_SUCCESS, $fd, $request, $response);
// Handshaking successful, Manually triggering the open event
Co::create(function () use ($psr7Req, $fd) {
$this->onOpen($psr7Req, $fd);
});
} catch (Throwable $e) {
Swoft::trigger(WsServerEvent::HANDSHAKE_ERROR, $e, $request);
/** @var WsErrorDispatcher $errDispatcher */
$errDispatcher = BeanFactory::getSingleton(WsErrorDispatcher::class);
// Handle handshake error
$psr7Res = $errDispatcher->handshakeError($e, $psr7Res);
$psr7Res->quickSend();
} finally {
// Defer
Swoft::trigger(SwoftEvent::COROUTINE_DEFER);
// Destroy
Swoft::trigger(SwoftEvent::COROUTINE_COMPLETE);
// Unbind cid => sid(fd)
Session::unbindCo();
}
return $status;
}
/**
* @param Psr7Request $request
* @param int $fd
*
* @throws Throwable
*/
public function onOpen(Psr7Request $request, int $fd): void
{
// Bind cid => sid(fd)
Session::bindCo((string)$fd);
$server = server()->getSwooleServer();
server()->log("Open: conn#{$fd} has been opened", [], 'debug');
try {
/** @var Connection $conn */
$conn = Session::mustGet();
$info = $conn->getModuleInfo();
if ($method = $info['eventMethods']['open'] ?? '') {
$class = $info['class'];
server()->log("Open: conn#{$fd} call ws open handler '{$class}::{$method}'", [], 'debug');
/** @var WsModuleInterface $module */
$module = BeanFactory::getSingleton($class);
$module->$method($request, $fd);
}
Swoft::trigger(WsServerEvent::OPEN_AFTER, $fd, $server, $request);
} catch (Throwable $e) {
Swoft::trigger(WsServerEvent::OPEN_ERROR, $e, $request);
/** @var WsErrorDispatcher $errDispatcher */
$errDispatcher = BeanFactory::getSingleton(WsErrorDispatcher::class);
$errDispatcher->openError($e, $request);
} finally {
// Unbind cid => sid(fd)
Session::unbindCo();
}
}
}