我正在尝试在我的网络服务器上创建一个网络套接字。我想从我的客户端连接到 websocket 以更新网站上的信息。但是,当我尝试连接时,我在 Postman 中收到以下错误:
错误:意外的服务器响应:101 握手细节 请求网址:https://example.com:8443/radar 请求方式:GET 状态代码:101 切换协议 请求标头 Sec-WebSocket-版本:13 连接:升级 升级:网络套接字 Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits 主机:example.com:8443 响应头 日期:2023 年 3 月 11 日星期六 02:20:55 GMT 服务器:阿帕奇/2 升级:网络套接字 X-Powered-By:Phusion Passenger 5.3.7 状态:101 切换协议 变化:用户代理 内容长度:2
要监听端口 8443,我的 VirtualHosts (httpd.conf) 中有以下内容:
Listen 8443
<VirtualHost 123.123.123.123:8443>
SSLEngine on
SSLCertificateFile /usr/local/directadmin/data/users/admin/domains/example.com.cert.combined
SSLCertificateKeyFile /usr/local/directadmin/data/users/admin/domains/example.com.key
ServerName www.example.com
ServerAlias www.example.com example.com
ServerAdmin [email protected]
DocumentRoot /home/admin/domains/example.com/private_html
UseCanonicalName OFF
<IfModule !mod_ruid2.c>
SuexecUserGroup admin admin
</IfModule>
CustomLog /var/log/httpd/domains/example.com.bytes bytes
CustomLog /var/log/httpd/domains/example.com.log combined
ErrorLog /var/log/httpd/domains/example.com.error.log
# Allow WebSocket connections
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/radar [NC]
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/radar/(.*)$ wss://localhost:8443/radar/$1 [P,L]
<Directory /home/admin/domains/example.com/private_html>
AllowOverride AuthConfig FileInfo Indexes Limit Options=Indexes,IncludesNOEXEC,MultiViews,SymLinksIfOwnerMatch,FollowSymLinks,None
Options -ExecCGI -Includes +IncludesNOEXEC
php_admin_flag engine ON
php_admin_value sendmail_path '/usr/sbin/sendmail -t -i -f [email protected]'
php_admin_value mail.log /home/admin/.php/php-mail.log
php_admin_value open_basedir /home/admin/:/tmp:/var/tmp:/opt/alt/php74/usr/share/pear/:/dev/urandom:/usr/local/lib/php/:/usr/local/php74/lib/php/
</Directory>
# Mail auto configuration (Thunderbird, MS Outlook)
ProxyPassMatch "^/\.well-known/autoconfig/mail/config-v1\.1\.xml$|^/([Aa]utodiscover)/\1\.xml$" "unix:/usr/local/directadmin/shared/internal.sock|http://localhost"
</VirtualHost>
NodeJS脚本的代码如下:
雷达.js:
const http = require('http');
const WebSocketServer = require('websocket').server;
const fs = require('fs')
function writeHtml(outputStream) {
const inputStream = fs.createReadStream('index.html')
inputStream.pipe(outputStream)
outputStream.on('finish', () => {
console.log('served')
outputStream.end();
})
outputStream.on('error', (e) => {
console.error('errors', e)
outputStream.end();
})
}
const server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
writeHtml(res)
})
server.listen(process.env.PORT || 8443);
const wsServer = new WebSocketServer({
httpServer: server
});
wsServer.on('request', function(request) {
console.log("ws received")
const connection = request.accept(null, request.origin);
connection.on('message', function(message) {
console.log('Received Message:', message.utf8Data);
connection.sendUTF('Hi this is WebSocket server!');
});
connection.on('close', function(reasonCode, description) {
console.log('Client has disconnected.');
});
});
index.html:
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Playground</title>
</head>
<body>
</body>
<script>
const ws = new WebSocket('wss://' + location.host);
ws.onopen = function() {
console.log('WebSocket Client Connected');
ws.send('Hi this is web client.');
};
ws.onmessage = function(e) {
console.log("Received: '" + e.data + "'");
};
</script>
</html>
我也试过修改 VirtualHosts,但没有任何效果。
如果能得到任何帮助,我将不胜感激,我也没有使用代理(如 Cloudflare 等)。谢谢。