简体   繁体   English

php / javascript-使用Ajax自动更新客户端

[英]php/javascript - using ajax to automatically update the client side

I am using PHP write the server side of the web page, and javascript to build the client side. 我正在使用PHP编写网页的服务器端,并使用javascript来构建客户端。

There is a div on the website that will show the data received from server side, for example, Let's say showing the current number of registered users in the databases. 网站上有一个div,它将显示从服务器端接收的数据,例如,假设显示了数据库中当前的注册用户数。 This number will keep changing in an unknown period of time . 这个数字将在未知的时间内不断变化。 For example, the number could be the same in 10 hours, and it could increment by 1000 in 10 minutes. 例如,该数字可以在10小时内相同,并且可以在10分钟内增加1000。

My first question is, what is the best approach to do this functionality so that website can always show the latest or almost latest value of this data? 我的第一个问题是,什么是实现此功能的最佳方法,以便网站始终可以显示此数据的最新值或几乎最新值?

My original approach is using Javascript AJAX and wrapped by setInterval(1000) (-1 second) to send Ajax request to the server side. 我的原始方法是使用Javascript AJAX并由setInterval(1000)(-1秒)包装,以将Ajax请求发送到服务器端。 But I am not sure if this is too heavy for client side (because javascript is single threaded, it might be slow if there is a function running within each 1 second) or even server side( for example, if hundreds of users open this website and hundreds of requests will be sent to server side). 但是我不确定这对于客户端来说是否太重(因为javascript是单线程的,如果在每1秒内运行一个函数,它可能会很慢)甚至在服务器端(例如,如果成百上千的用户打开此网站)并将数百个请求发送到服务器端)。 So is this ok? 这样可以吗?

My second question is, if this is not a good approach, what else can I do to achieve the same goal. 我的第二个问题是,如果这不是一个好的方法,那么我还能做些什么来实现相同的目标。 I was thinking about using the other way around. 我当时正在考虑使用另一种方法。 For example, if there is update in the server side, then use PHP in the server side to push this update to client side, or even directly change the data shown in the HTML using PHP scripting. 例如,如果服务器端有更新,则在服务器端使用PHP将更新推送到客户端,甚至使用PHP脚本直接更改HTML中显示的数据。 Is this possible? 这可能吗? if it is, is this a good approach? 如果是,这是一个好方法吗?

Any codes or examples provided would be really appreciated. 提供的任何代码或示例将不胜感激。

You can use simple long polling . 您可以使用简单的长轮询

The client side requests a script "/counter?oldvalue=...&timestamp=... . The server does not immediately return a value if it was not changed, but idles for up to 30 seconds. 客户端请求脚本"/counter?oldvalue=...&timestamp=...如果未更改,服务器不会立即返回一个值,但是会闲置长达30秒。

If the counter value gets changed, then you return the updated value immediately. 如果计数器值被更改,则您将立即返回更新的值。

You can certainly use AJAX for updates that are spread out over time, but if you want to connect to your server every second or so, I would advice against it. 当然,您可以使用AJAX进行随着时间推移而更新的更新,但是如果您希望每秒钟左右连接到服务器,我建议您不要这样做。

You have a couple of options here: 您可以在此处选择两个选项:

  • As @Kay already mentioned, long polling might be an option. 正如@Kay已经提到的那样, long polling可能是一种选择。

  • Alternatively, you might want to look into WebSockets or a framework that supports WebSockets, like Meteor 另外,您可能希望研究WebSockets或支持WebSockets的框架,例如Meteor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM