长轮询导致服务器问题?
我终于制作了一个简单的聊天页面,这是我长期以来一直想做的,但我的服务器遇到了问题。
我不确定长轮询是否是正确的术语,但根据我的理解,我认为是的。我有一个对 php 页面的 ajax 调用,该页面检查 mysql 数据库中是否有比 ajax 请求中发送的时间更新的消息。如果没有更新的消息,它将继续循环并检查,直到出现为止。否则,它只返回新消息,客户端脚本在收到消息后立即发送另一个 ajax 请求。
一切工作正常,除了 000webhost 上的服务器在几条聊天消息后停止响应的部分,以及 x10 托管上的服务器给我一条有关达到资源限制的消息。
也许这是一种愚蠢的聊天系统方法,但这是我所知道的。如果有更好的方法请告诉我。
编辑:天哪,我刚刚想到我没有在服务器上的 while 循环中放置任何睡眠时间。
I've finally made a simple chat page that I had wanted to make for a while now, but I'm running into problems with my servers.
I'm not sure if long polling is the correct term, but from what I understand, I think it is. I have an ajax call to a php page that checks a mysql database for messages with times newer than the time sent in the ajax request. If there isn't a newer message, it keeps looping and checking until there is. Else, it just returns the new messages and the client script sends another ajax request as soon as it gets the messages.
Everything is working fine, except for the part where the server on 000webhost stops responding after a few chat messages, and the server on x10 hosting gives me a message about hitting a resource limit.
Maybe this is a dumb way to do a chat system, but it's all I know how to do. If there is a better way please let me know.
edit: Holy hell, it's just occurred to me that I didn't put any sleep time in the while loop on the server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以找到很多关于此的阅读材料,但我不相信免费网络托管将允许您做您想做的事情。 PHP 也并不是真正为创建聊天系统而设计的。
我建议使用 WebSockets,例如使用 Node.JS 与 Socket.IO 或 Tornado 与Python;有很多解决方案,但其中大多数都要求您运行自己的服务器,因为它需要运行一个同时与多个连接交互的整个程序,而不是仅以单个连接开始和结束的简单脚本。
You can find a lot of reading on this, but I disbelieve that free web hosting is going to allow to do what you are thinking of doing. PHP was also not really designed to create chat systems.
I would recommend using WebSockets, and use for example, Node.JS with Socket.IO, or Tornado with Python; There is a lot of solutions out there, but most of them would require you to run your own server since it requires to run a whole program that interacts with many connections at once instead of simple scripts that just start and finish with a single connection.
无论服务器上是否有更新的消息,都使用相同的策略怎么样?服务器将始终返回较新消息的列表 - 当没有较新消息时此列表可能为空。空列表也可以被编码为特殊的数据令牌。
然后,客户端在这两种情况下以相同的方式进行:处理接收到的数据并在一段时间后请求新消息。
What about using the same strategy whether there are newer messages on the server or not. The server would always return a list of newer messages - this list could be empty when there are no newer messages. The empty list could be also be encoded as a special data token.
The client then proceeds in both cases the same way: it processes the received data and requests new messages after a time period.
确保你在每个循环上睡眠(1)你的代码,代码每秒会进入循环几次,给你的数据库/服务器带来压力。
但是,nodejs 或 websockets 仍然是处理实时聊天的更好技术。
Make sure you sleep(1) your code on each loop, the code gonna enter the loop several times per second, stressing your database/server.
But still, nodejs or websockets are better tecnologies to deal with real time chats.