通过websocket将聊天添加到现有的PHP Web应用程序 [英] Adding chat through websocket, to an existing PHP web app

查看:82
本文介绍了通过websocket将聊天添加到现有的PHP Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在标准PHP/Apache平台上已有一个Web应用程序.现在,我想要添加聊天功能,并且希望它可以在websocket上实时显示,并且为此,我对nodejs上的socket.io进行了一些研究.因此,除了运行大型PHP应用程序的Apache外,我还要让带有socket.io的nodejs运行聊天.

I have an existing web application on the standard PHP/Apache platform. Now, what I want is to add chat functionality, and I want it to be real-time over a websocket, and to that extend I've studied socket.io on nodejs a little bit. So in addition to Apache running the big PHP app, I would have nodejs with socket.io running chat.

但是我真正不了解的是,如何识别nodejs聊天代码中的用户?首先,Apache和nodejs将无法在同一端口上运行,这意味着我将在端口8080上运行聊天,在这种情况下,我将丢失用户的cookie,这意味着我现在必须询问他们如果他们想使用聊天功能,可以再次在这个由nodejs驱动的端口上再次登录?似乎很荒谬,但我不知道该怎么做.

But what I don't really understand, is how would I recognise my users in the code for nodejs chat? For one, Apache and nodejs won't be able to run on the same port, meaning I'll run chat on port 8080 for example, in which case I lose the user's cookies, which then means I now have to ask them to log in once more on this nodejs-powered port if they want to use chat? Seems ridiculous, but I don't know which way to go about it.

当然,我不能将我的全部代码移植到nodejs上.因此,理想情况下,我希望Apache和Node.js共存.或者我只是完全误解了聊天应该在Web应用程序中如何工作.

I cannot port my entire code onto nodejs, of course. So ideally I would want Apache and nodejs to coexist. Or I am just completely misunderstanding how chat is supposed to work in web apps.

任何提示都值得赞赏.

推荐答案

例如,您可以在PHP上运行Apache.端口3001和端口3002上的Node应用程序,并将nginx配置为反向代理,以使其在端口80上均可用,例如,根目录/中的PHP应用程序和/chat目录中的Node应用程序,使用Nginx的配置是这样的:

You can run your Apache with PHP on e.g. port 3001 and your Node app on port 3002, and have nginx configured as a reverse proxy to make them both available on port 80, for example your PHP app in the root / directory and your Node app in the /chat directory, with nginx config like this:

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会稍微复杂一些,但不会那么多:

With SSL it would be slightly more complicated but not that much:

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应用程序甚至可以在不同的服务器上运行-只需在nginx配置中使用它们的地址即可.

Your PHP and Node apps could even run on different servers - just use their addresses in the nginx config.

有关更多详细信息,请参见此答案及其评论:

See this answer and its comments for more details:

这篇关于通过websocket将聊天添加到现有的PHP Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆