|
<?php |
|
$server = new Swoole\WebSocket\Server("0.0.0.0", 9503); |
|
|
|
$server->on('message', function () { |
|
}); |
|
|
|
$server->on('handshake', function (\Swoole\Http\Request $request, \Swoole\Http\Response $response) { |
|
|
|
$secWebSocketKey = $request->header['sec-websocket-key']; |
|
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#'; |
|
if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) { |
|
$response->end(); |
|
return false; |
|
} |
|
echo $request->header['sec-websocket-key']; |
|
$key = base64_encode(sha1( |
|
$request->header['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', |
|
true |
|
)); |
|
|
|
$headers = [ |
|
'Upgrade' => 'websocket', |
|
'Connection' => 'Upgrade', |
|
'Sec-WebSocket-Accept' => $key, |
|
'Sec-WebSocket-Version' => '13', |
|
'Set-Cookie' => 'hello=world', |
|
'Set-Cookie2' => 'hello=world', |
|
'Set-Cookie3' => 'hello=world', |
|
'Set-Cookieeeeeeee' => 'hello=world', |
|
'Set-Cookie4' => 'hello=world', |
|
'Set-Cooki' => 'hello=world', |
|
'Set-Cook' => 'hello=world', |
|
]; |
|
|
|
// WebSocket connection to 'ws://127.0.0.1:9502/' |
|
// failed: Error during WebSocket handshake: |
|
// Response must not include 'Sec-WebSocket-Protocol' header if not present in request: websocket |
|
if (isset($request->header['sec-websocket-protocol'])) { |
|
$headers['Sec-WebSocket-Protocol'] = $request->header['sec-websocket-protocol']; |
|
} |
|
|
|
foreach ($headers as $key => $val) { |
|
$response->header($key, $val); |
|
} |
|
|
|
$response->status(101); |
|
$response->end(); |
|
}); |
|
|
|
$server->start(); |