Я пытаюсь настроить супер простой веб-сокет на PHP + nginx
Nginx
server {
listen 8443 ssl;
server_name websocket.example.com;
root /var/www/public/websocket;
ssl_certificate /var/ini/ssl/public.crt;
ssl_certificate_key /var/ini/ssl/private.key;
error_log /var/log/nginx/error_websocket.log warn;
location / {
proxy_pass http://127.0.0.1:9000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
}
клиент
var host = 'wss://websocket.dyntest.dk:8443';
var socket = new WebSocket(host);
socket.onmessage = function(e){
document.getElementById('ws_test').innerHTML = e.data;
console.log(e.data);
};
Сервер
$address = '127.0.0.1';
$port = 9000;
// Create WebSocket.
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($server, $address, $port);
socket_listen($server);
$client = socket_accept($server);
// Send WebSocket handshake headers.
$request = socket_read($client, 5000);
preg_match('#Sec-WebSocket-Key: (.*)\r\n#', $request, $matches);
$key = base64_encode(pack(
'H*',
sha1($matches[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
));
$headers = "HTTP/1.1 101 Switching Protocols\r\n";
$headers .= "Upgrade: websocket\r\n";
$headers .= "Connection: Upgrade\r\n";
$headers .= "Sec-WebSocket-Version: 13\r\n";
$headers .= "Sec-WebSocket-Accept: $key\r\n\r\n";
socket_write($client, $headers, strlen($headers));
// Send messages into WebSocket in a loop.
while (true){
sleep(1);
$content = 'Now: ' . time();
$response = chr(129) . chr(strlen($content)) . $content;
socket_write($client, $response);
}
запуск сервера websocket
php -q public/websocket/index.php
ошибка
В браузере: wss://websocket.example.com:8443/
ERR_DISALLOWED_URL_SCHEME