dongqiao9394 2013-11-04 17:34
浏览 44
已采纳

使用websockets以及PHP和MySQL脚本的最佳方法是什么? [关闭]

What is the best way to use websockets along with PHP and MySQL scripts?

If I want to add a live chat to my PHP site, or store vars to the MySQL database when doing X action in real time (for a RPG website trading, for example).

I have found Ratchet, it's a good choice to achieve those things efficiently? Maybe using Node.js and PHP at the same time? I'm not sure what to do.

  • 写回答

1条回答 默认 最新

  • duancuan6466 2013-11-04 18:11
    关注

    I had the same question a few months ago, and answered it for myself after some tinkering and research.

    I used Node.js in combination with PHP.

    Basically, it works like this. Apache/php send a page to the client on request. It contains an auth token. The browser Initializes socket.io and sends the token back to a node js server. Node then sends the token to a private API (if you will) to the Apache server requesting needed context on the user (user id for example). When node gets this back, it stores it in a "connection ID" => "user ID" table. (or routing table (fancy specific term for a hash map))

    If a user sends a "chat" message to another user, it goes to Apache via ajax, does the database/processing stuff, then passes it back to Node.js on the back-end which plugs the user id into the routing table and sends the message to the users browser.

    I cant post code because this may be used in production at some point but that Is how I did it with a LAMP based server.

    NOTES: You will need to have a string account management system implemented for this to work, and I implemented this concept for my own account handling system so it is very application specific in my case.

    The need for the token is for security. This token for example could be a sessions ID. Something that can derive the user ID on the back-end, but not on the front end. It should change periodically. This way, if it is compromised, the attacker wont be able to connect to the live session for long, or at all.

    This concept can be improved. Such as a per-request token, that changes on every request that may only be used once. This is currently something I am working on with my live notification system.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?