dongqiao9394
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.

图片转代码服务由CSDN问答提供 功能建议

使用websockets以及PHP和MySQL脚本的最佳方法是什么? < p>如果我想在我的PHP站点上添加实时聊天,或者在实时执行X操作时将变量存储到MySQL数据库(例如,对于RPG网站交易)。

我找到了棘轮,这是有效实现这些目标的不错选择吗? 也许同时使用Node.js和PHP? 我不知道该怎么办。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duancuan6466
    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.

    点赞 评论

相关推荐