php棘轮websocket SSL连接?

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文件中启用这些模块:

  1. mod_proxy.so
  2. mod_proxy_wstunnel.so

将此设置添加到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套接字连接)已打开.

  • 很好,这可行,但一个问题:为什么"/ NNN"到底?谢谢! (2认同)
  • 引用我们的下一个 URL 参数(例如:/wss2/param1/param2...),我们的系统可以使用它来识别请求目的。如果不需要,您可以将其删除,但请注意,您必须具有相同的匹配身份 url 参数(在本例中为“/wss2/”),以便服务器能够匹配/捕获请求并将其转发到自定义域/端口。 (2认同)
  • 作为后续:我终于使用 https 在 Chrome 和 Firefox 的 localhost 上运行了这个,但在我的情况下,我不得不使用端口 8080 ......我对使用哪个端口感到困惑,这导致了很多尝试和错误。主要是错误。这对我有用:`var conn = new WebSocket('wss://localhost/wss2/');``ProxyPass /wss2/ ws://localhost:8080/` `$server = IoServer::factory( new HttpServer( new WsServer( new Chat() ) ), 8080 );` (2认同)

mat*_*exx 11

问题是React(建立在Ratchet上)不支持直接SSL连接.看到这个问题.

有一个简单的解决方法.使用带有以下配置的stunnel:

[websockets]
accept = 8443
connect = 8888
Run Code Online (Sandbox Code Playgroud)

Stunnel将在端口8443上处理SSL流量并将它们移植到您的websocket服务器.

  • 看起来从 2017 年初开始就支持 SSL。请参阅 [reactphp/socket#55](https://github.com/reactphp/socket/pull/55) 和 [@Jordi 的回答](https://stackoverflow.com) /a/47200806/759866)如下。 (3认同)

Son*_*ngo 9

我发现这个答案棘轮的谷歌集团克里斯·博登:

最好的解决方案是使用Nginx作为您的Web服务器.让Nginx在端口80上侦听传入连接并让它处理您的SSL.Nginx会将传入连接转发到常规网站的PHP-FPM,如果它检测到连接,则WebSocket连接会将其代理到您选择的端口上运行的Ratchet应用程序.然后你的javascript可以通过wss://mydomain.org连接

如果您的应用程序将使用nginx提供,这是使用stunnel的另一种方法.

  • 有这个技巧的nginx配置示例吗? (3认同)
  • @temuri我认为这是要点https://gist.github.com/octaflop/4991052 (2认同)
  • 一个改进的尝试:http://stackoverflow.com/questions/22493646/ratchet-nginx-ssl-secure-websocket (2认同)

Jor*_*rdi 9

几天前,我在寻找这个问题的答案,并在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

  • 这是最新的答案,既然添加了 SSL 支持,可能应该是被接受的答案! (3认同)

Rai*_*nch 5

如果您使用的是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开放给世界。