将Websocket与PHP和MySQL脚本一起使用的最佳方法是什么? [英] What is the best way to use websockets along with PHP and MySQL scripts?

查看:308
本文介绍了将Websocket与PHP和MySQL脚本一起使用的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结合使用websocket和PHP和MySQL脚本的最佳方法是什么?

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

如果我想在我的PHP网站上添加实时聊天,或者在实时执行X动作时(例如,对于RPG网站交易)将vars存储到MySQL数据库.

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

我已经找到了 Ratchet ,这是有效实现这些目标的好选择吗?也许同时使用Node.js和PHP?我不确定该怎么办.

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.

推荐答案

几个月前,我有一个相同的问题,经过一番修改和研究后,我自己回答了这个问题.

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

我将Node.js与PHP结合使用.

I used Node.js in combination with PHP.

基本上,它是这样工作的. Apache/php根据请求将页面发送到客户端.它包含一个身份验证令牌. 浏览器初始化socket.io并将令牌发送回节点js服务器.然后,Node将令牌发送给Apache服务器的私有API(如果愿意),以请求用户所需的上下文(例如,用户ID).当节点将其取回时,它将其存储在"connection ID" => "user ID"表中. (或路由表(哈希表的花式特定术语))

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))

如果用户向另一个用户发送聊天"消息,则该消息会通过ajax发送到Apache,执行数据库/处理工作,然后将其传递回后端的Node.js,从而将用户ID插入到路由表并将消息发送到用户浏览器.

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.

我无法发布代码,因为它有时可能会在生产中使用,但这就是我使用基于LAMP的服务器所做的事情.

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.

令牌的需要是为了安全.例如,此令牌可以是会话ID.可以在后端而非前端上导出用户ID的东西.它应该定期更改.这样,如果遭到破坏,攻击者将无法长时间或根本无法连接到实时会话.

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.

这篇关于将Websocket与PHP和MySQL脚本一起使用的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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