我正在尝试测试简单的ping&使用Ratchet通过WebSockets进行响应。我的WebSocket服务器不看到来自我的Web浏览器的WebSocket客户端的任何响应 但WebSocket服务器发送的 数据包被浏览器看得很清楚。我想找到原因。
我目前的猜测是:
wsclient.send(encodeFrame("{'action': 'pong'}"));
127.0.0.1:65000
运行的WebSocket服务器(Ratchet)/api
重定向到127.0.0.1:65000
(棘轮)127.0.0.1:65000
工作正常<Amazon NAT IP of instance>:80
工作正常<Amazon public IP of instance>:80
工作正常<CloudFlare IP of Amazon public IP>:80
在应用程序实现级别上连接到WebSocket服务器,但在任何级别(App,WsServer,HTTPServer)上都看不到onMessage
方法上的数据包<CloudFlare IP of Amazon public IP>:443
提供400 Bad Request
,因为测试客户端只是简单的TCP流直接连接到CloudFlare缓存IP提供的主机。 Dojox.Socket连接到wss://host/api
。在Ratchet上的应用程序实现级别上再次看到连接(onOpen
被触发)。我的浏览器看到ping
数据包很好,所以从Ratchet发送的工作正常。
但是我尝试从浏览器向pong
发送ping
回复,而且onMessage
方法永远不会在Ratched上的任何级别上被触发。连接保持打开状态,如果我用Fiddler观看,不断发送ping和pongs,但WebSocket服务器从不接收这些pongs(onMessage
)。
根据Fiddler的WebSocket流显示,来自浏览器的pongs具有“按键掩盖的数据:<some hex>
”,但来自Ratched的ping 不已屏蔽。
页面加载:
本地计算机http://host/→CloudFlare HTTPS重定向https://host/→http://host-with-amazon-public-ip/→http://host-with-amazon-NAT-ip/→HTML + JS页面,将WebSocket连接加载到/api
与/api
的WebSocket连接:
CloudFlare HTTPS已重定向到wss:// host / api→http://host-with-amazon-public-ip/api→http://host-with-amazon-NAT-ip/api→本地服务器nginx重定向/ api→127.0.0.1:65000
→连接升级→WebSocket流→用于Web浏览器的WebSocket流
server {
listen 80;
server_name test.example.com;
root /home/raspi/test/public;
autoindex off;
index index.php;
access_log /home/raspi/test/http-access.log;
error_log /home/raspi/test/http-error.log notice;
location / {
index index.php;
try_files $uri $uri/ /index.php?$args;
}
location /api {
access_log /home/raspi/test/api-access.log;
error_log /home/raspi/test/api-error.log;
expires epoch;
proxy_ignore_client_abort on;
proxy_buffering off;
proxy_request_buffering off;
proxy_cache off;
proxy_pass http://127.0.0.1:65000/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Connection "keep-alive, Upgrade";
proxy_set_header Upgrade "websocket";
proxy_set_header Accept-Encoding "gzip, deflate";
proxy_set_header Sec-WebSocket-Extensions "permessage-deflate";
proxy_set_header Sec-WebSocket-Protocol "game";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ^~ /js/app/ {
try_files $uri /;
expires epoch;
add_header Cache-Control "no-cache" always;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
try_files $uri /;
access_log off;
expires max;
}
location = /robots.txt { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; }
location ~ /\. { access_log off; log_not_found off; deny all; }
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
答案 0 :(得分:0)
Web套接字目前仅在业务和企业级别提供。你提到你是免费的计划。这就是问题所在。
答案 1 :(得分:0)
问题是您的提供商拒绝连接和更新标头。您可以转到 Ratchet / WebSocket / Version / RFC6455 / HandshakeVerifier.php 并将代码修改为:
...
public function verifyAll(RequestInterface $request) {
$passes = 0;
$passes += (int)$this->verifyMethod($request->getMethod());
$passes += (int)$this->verifyHTTPVersion($request->getProtocolVersion());
$passes += (int)$this->verifyRequestURI($request->getPath());
$passes += (int)$this->verifyHost((string)$request->getHeader('Host'));
$passes += (int)$this->verifyUpgradeRequest((string)$request->getHeader('Upgrade'));
$passes += (int)$this->verifyConnection((string)$request->getHeader('Connection'));
die((string)$request);
...
您会看到结果请求没有必填字段;
解决方法:覆盖WsServer :: onOpen函数并将这些字段添加到手动请求。但这是不安全的......