通过websocket添加聊天,到现有的PHPnetworking应用程序

我在标准的PHP / Apache平台上有一个现有的Web应用程序。 现在,我想要的是添加聊天function,我希望它是实时的通过websocket,并扩展我已经研究了一点nodeJs上的socket.io。 因此,除了运行大型PHP应用程序的Apache之外,我还会使用socket.io运行聊天的nodejs。

但是我真的不明白的是,如何在nodejs聊天的代码中识别我的用户呢? 首先,Apache和nodejs将无法在同一个端口上运行,这意味着我将在端口8080上运行聊天,例如,在这种情况下,我丢失了用户的cookies,这意味着我现在必须要求他们login再次在这个nodejs-powered端口,如果他们想使用聊天? 看起来很荒谬,但是我不知道要走哪条路。

当然,我无法将我的整个代码移植到nodejs上。 所以理想情况下,我会希望Apache和nodejs共存。 或者我完全误解了聊天应该如何在networking应用程序中工作。

任何提示赞赏。

你可以在例如3001端口上使用PHP运行你的Apache,在3002端口上运行你的Node应用程序,并且将nginxconfiguration为反向代理,使它们都可以在端口80上使用,例如你的PHP应用程序在root /目录和你的Node应用程序在/chat目录中,像这样的nginxconfiguration:

 server { listen 80; server_name example.com; location /chat { proxy_pass http://localhost:3002; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; } location / { proxy_pass http://localhost:3001; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; } } 

有了SSL,它会稍微复杂一些,但不是那么多:

 server { listen 443; server_name example.com; add_header Strict-Transport-Security "max-age=3600"; ssl on; ssl_certificate /.../chained2.pem; ssl_certificate_key /.../domain.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA; ssl_session_cache shared:SSL:50m; ssl_prefer_server_ciphers on; location /chat { proxy_pass http://localhost:3002; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; } location / { proxy_pass http://localhost:3001; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; } } 

您的PHP和Node应用程序甚至可以在不同的服务器上运行 – 只需在nginxconfiguration中使用它们的地址即可。

看到这个答案及其评论更多的细节:

  • 在express上使用ngix和ssl实现的反向代理失败