vie*_*n09 37 php ssl websocket ratchet
我有一个棘轮聊天服务器文件
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use MyAppChat\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new WsServer(
new Chat()
)
, 26666
);
$server->run();
Run Code Online (Sandbox Code Playgroud)
我使用Websocket连接,ws它工作正常
if ("WebSocket" in window) {
var ws = new WebSocket("ws://ratchet.mydomain.org:8888");
ws.onopen = function() {
// Web Socket is connected. You can send data by send() method.
ws.send("message to send");
};
ws.onmessage = function (evt) {
var received_msg = evt.data;
};
ws.onclose = function() {
// websocket is closed.
};
} else {
// the browser doesn't support WebSocket.
}
Run Code Online (Sandbox Code Playgroud)
我想要安全连接,所以我尝试连接SSL,但不起作用.
if ("WebSocket" in window) {
var ws = new WebSocket("wss://ratchet.mydomain.org:8888");
ws.onopen = function() {
// Web Socket is connected. You can send data by send() method.
ws.send("message to send");
};
ws.onmessage = function (evt) {
var received_msg = evt.data;
};
ws.onclose = function() {
// websocket is closed.
};
} else {
// the browser doesn't support WebSocket.
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何连接websocket与SSL连接
任何的想法?
web*_*der 41
如果您使用的是Apache Web服务器(2.4或更高版本),请在httpd.conf文件中启用这些模块:
将此设置添加到httpd.conf文件中
ProxyPass /wss2/ ws://ratchet.mydomain.org:8888/
Run Code Online (Sandbox Code Playgroud)
如果需要WSS连接,请在JavaSscript调用中使用此URL:
var ws = new WebSocket("wss://ratchet.mydomain.org/wss2/NNN");
Run Code Online (Sandbox Code Playgroud)
在应用设置(telnet主机名端口)之前,重新启动Apache Web服务器并确保您的Ratchet worker(Web套接字连接)已打开.
mat*_*exx 11
问题是React(建立在Ratchet上)不支持直接SSL连接.看到这个问题.
有一个简单的解决方法.使用带有以下配置的stunnel:
[websockets]
accept = 8443
connect = 8888
Run Code Online (Sandbox Code Playgroud)
Stunnel将在端口8443上处理SSL流量并将它们移植到您的websocket服务器.
最好的解决方案是使用Nginx作为您的Web服务器.让Nginx在端口80上侦听传入连接并让它处理您的SSL.Nginx会将传入连接转发到常规网站的PHP-FPM,如果它检测到连接,则WebSocket连接会将其代理到您选择的端口上运行的Ratchet应用程序.然后你的javascript可以通过wss://mydomain.org连接
如果您的应用程序将使用nginx提供,这是使用stunnel的另一种方法.
几天前,我在寻找这个问题的答案,并在Github Ratchet问题中找到了这个问题:https : //github.com/ratchetphp/Ratchet/issues/489
由heidji回答的最后一个答案说:
我只是为像我这样的新手添加了此注释,这些新手需要快速说明如何实现SSL:通过ReactPHP文档,您只需要以这种方式构建提到的SecureServer:
$webSock = new React\Socket\Server('0.0.0.0:8443', $loop);$webSock = new React\Socket\SecureServer($webSock, $loop, ['local_cert' => '/etc/ssl/key.pem', 'allow_self_signed' => true, 'verify_peer' => false]);
然后按上述cboden所述注入IoServer
因此,似乎现在有了一种无需使用HTTPS代理即可通过Ratchet实现安全的Websocket服务器的方法。
这里有SecureServer类文档:https : //github.com/reactphp/socket#secureserver
如果您使用的是Nginx,只需在SSL服务器块中编写以下代码:
location /services/myservice {
# switch off logging
access_log off;
# redirect all HTTP traffic to localhost
proxy_pass http://localhost:1234;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# WebSocket support (nginx 1.4)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Path rewriting
rewrite /services/myservice/(.*) /$1 break;
proxy_redirect off;
# timeout extension, possibly keep this short if using a ping strategy
proxy_read_timeout 99999s;
}
Run Code Online (Sandbox Code Playgroud)
这会将所有wss://yoursite.com/services/myservice呼叫升级到在端口1234上运行的套接字。只需确保记住不要将端口1234开放给世界。
| 归档时间: |
|
| 查看次数: |
32886 次 |
| 最近记录: |