PHP 共享主机上的 WebSockets
我一直在研究显示“在线用户”计数器的最佳方式,该计数器已更新到第二个,试图避免连续的ajax轮询。
显然 WebSockets 似乎是最好的选择。由于这是一个 Intranet,我将要求使用 Chrome 或 Safari,因此不应该存在兼容性问题。
自从我刚接触 WebSockets 以来,我一直在阅读一些有关 WebSockets 的文章,并且我想我非常了解它是如何工作的。
我不太确定如何用 PHP 实现它。 Node.js 似乎是这种情况的自然选择,因为它具有“始终运行”的特性,但这不是一个选择。
为什么我最困惑的是 PHP 运行时,当它完成时,它就结束了。如果PHP结束了,socket连接不是会丢失吗?或者如果 php 重新运行它会通过 ip 查找用户? (我认为不太可能)
然后我找到了这个图书馆 http://code.google.com/p/phpwebsocket/ 但它似乎有点旧(它提到只有 Chrome nightly 与 WebSockets 兼容),
其中有一点说“从命令行,运行 server.php 程序来侦听套接字连接”。这意味着我需要 SSH,这是许多共享托管计划所没有的。
我的另一个疑问是该库源代码中的另一行:
set_time_limit(0);
这是否意味着 php 文件将连续运行?这允许共享托管吗?据我所知,所有主机都会在 1 o2 分钟超时后杀死 php。
我有一个包含在线用户的 mysql 表,我想使用 PHP 通过 websocket 向这些在线用户广播登录用户的数量。有人可以帮助我或向我指出如何实现这一目标的更好信息吗?
谢谢
I've been doing some research of the best way to show an "users online" counter which is updated to the second trying to avoid continuos ajax polling.
Obviously WebSockets seems to be the best option. Since this is an intranet I will make it a requirement to use Chrome or Safari so there shouldn't be compatibility issues.
I've been reading some articles about WebSockets since I'm new to it and I think I pretty much understand how it works.
What I'm not so sure is how to implement it with PHP. Node.js seems the natural choice for this because of it's "always running" nature but that's not an option.
Why I'm most confused about is the fact that PHP runs and when it's done, it ends. If PHP ended, wouldn't the socket connection be lost? Or if the php re-runs it will look back the user by ip? (I don't see that likely)
Then I found this library
http://code.google.com/p/phpwebsocket/
but it seems to be a little old (it mentions only Chrome nightly is compatible with WebSockets)
In one point says "From the command line, run the server.php program to listen for socket connections." which means I need SSH, something many shared hosting plans don't have.
And my other doubt is this other line in the source of that library:
set_time_limit(0);
does that mean that the php file will run continuously? Is that allow in shared hosting? From what I know all hostings kill php after a timeout of 1 o2 minutes.
I have a mysql table with online users and I want to use PHP to broadcast via websocket the amount of logged in users to those online users. Can someone please help me or point me somewhere with better information how this could be achieved?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使在专用托管上,Websocket 也需要很多东西,抛开共享托管不谈。
对于您的要求,服务器发送事件(sse)是正确的选择,因为只有服务器才会将数据推送到客户端。
SSE可以简单地调用服务器脚本,很像ajax,但是客户端会接收并能够在数据到来时一部分部分地处理数据。每当有数据到来时就会生成Dom事件。
但是IE不支持SSE即使在版本 10 中也是如此。因此,对于 IE,您必须使用一些后备技术,例如“永远的 iframe”。
就托管而言,普通的共享托管(以及那些不是很便宜的托管)将允许 php 脚本长时间运行,只要它们不被视为问题即可。
Websockets would require lots of thing even on dedicated hosting, put aside shared hosting.
For your requirement server sent events (sse) is the correct choice, since only the server will be pushing data to the client.
SSE can simply call a server script, very much like ajax, but the client side will receive and be able to process data part by part as it comes in. Dom events would be generated whenever some data comes in.
But IE does not support SSE even in version 10. So for IE you have to use some fallback technique like "foreever iframe".
as far as hosting is concerned, ordinary shared hostings (and those which are not very cheap) would allow php scripts to run for long, as long as they are not seen as a problem.