JS/PHP“监听器"触发事件 [英] JS/PHP 'listener' to trigger an event

查看:37
本文介绍了JS/PHP“监听器"触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PHP 制作一个应用程序,我想构建一个非常简单的页面,如果 MySQL 行中的两个值设置为1"(真),则会播放声音.

I'm making an application using PHP, and I wanted to build a really simple page where a sound is played if two values in a MySQL row are set to '1' (true).

例如,如果没有或只有一个值设置为 1,则脚本不会播放声音.

For instance, if none or only one of the values is set to 1, the script does not play the sound.

然而,一旦第二个值变为 1,就会有一个 JS 'listener' 检测到并自动播放声音.

However, as soon as the second value changes to 1, there should be a JS 'listener' which detects this and automatically plays the sound.

有什么办法可以实现吗?

Is there any way to achieve this?

推荐答案

可以使用 webSockets 通过向客户端推送消息来完成.这是在php中实现webSockets的解决方案.

It can be done using webSockets by pushing a message to client side. here is a solution for implementing webSockets in php.

对于如何在 PHP 中创建 websockets 服务器,如果您想自己动手,这里也是一个很好的答案.但是我可以告诉你,如果你的用户群很大,使用 php 作为 webSocket 服务器不会很好地扩展.您可能需要考虑使用 Node.js.

Also here is a good answer for How to create websockets server in PHP if you want to do it yourself. But I can tell you that using php as webSocket server is not going to scale well if your user base is huge. You may want to consider doing it using Node.js.

另一种选择:您还可以不时使用 ajax 检查更新.但它不会像 webSocket 解决方案那样即时.在此解决方案中,服务器将每 x 秒为每个用户接收 1 个请求.

Another option: You can also check for update using ajax from time to time. but it will not be as instantaneous as webSocket solution. In this solution the server will receive 1 req per user each x seconds.

这篇关于JS/PHP“监听器"触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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