Reverse AJAX吗?可以将数据更改“推送”到脚本中吗?

时间:2021-10-26 15:53:20

I have noticed that some of my ajax-heavy sites (ones I visit, not ones I have built), have certain auto-refresh features. For example, in GMail, if I get a new message, I see the new message without a page reload. It's the same with the Facebook browser-based IM client. From what I can tell, there aren't any java applets handling the server-browser binding, so I'm left to assume it's being done by AJAX and perhaps some element I'm unaware of. So by my best guess, it's done in one of two ways:

我注意到我的一些ajax为主的站点(我访问的站点,而不是我构建的站点)具有某些自动刷新功能。例如,在GMail中,如果我得到一个新消息,我将看到新的消息,而不需要重新加载页面。基于Facebook浏览器的IM客户端也是如此。据我所知,没有任何java applet处理服务器-浏览器绑定,所以我只能假设它是由AJAX完成的,可能还有一些我不知道的元素。所以我猜,它有两种方法:

  1. The javascript does a steady "ping" to a server-side script, checking for any updates that might be available (which would explain why some of these pages bring any other heavy-duty pages to a crawl). or

    javascript对服务器端脚本进行稳定的“ping”,检查可能可用的任何更新(这可以解释为什么这些页面中有一些会带来其他繁重的页面)。或

  2. The javascript sits idly by and a server-side script actually "Pushes" any updates to the browser. But I'm not sure if this is possible. I'd imagine there is some kind of AJAX function that still pings, but all it simply asks "any updates?" and the server-script has a simple boolean that says "nope" or "I'm glad you asked." But if this is the case, any data changes would need to call the script directly so that it has the data changes ready and makes the change to that boolean function.

    javascript闲置,服务器端脚本实际上“推动”浏览器的任何更新。但我不确定这是否可能。我可以想象有某种AJAX函数仍然在ping,但它只会问“有什么更新吗?”而服务器脚本有一个简单的布尔值,它说“没有”或“我很高兴你问了”。但如果是这种情况,任何数据更改都需要直接调用脚本,以便它准备好数据更改并对该布尔函数进行更改。

So is that possible/feasible/how it works? I imagine something like:

这是可能的吗?我想这样:

Someone sends an email/IM/DB update to the server, the server calls the script using the script's URL plus some relevant GET variable, the script notes the change and updates the "updates available" variable, the AJAX gets the response that there are in fact updates, the AJAX runs its normal "update page" functions, which executes the normal update scripts and outputs them to the browser.

某人发送一个电子邮件/ IM / DB更新到服务器,服务器调用脚本使用脚本的URL加上一些相关变量,该脚本指出,改变和更新“更新可用”变量,AJAX响应,事实上有更新,AJAX运行正常的“更新页面”功能,将执行正常的更新脚本,输出到浏览器。

I ask because it seems really inefficient that the js is just doing a constant check which requires a) the server to do work every 1.5 seconds, and b) my browser to do work every 1.5 seconds just so that on my end I can say "Oh boy, I got an IM! just like a real IM client!"

我问,因为它似乎非常低效的js只是做一个常数检查需要)服务器工作每1.5秒,和b)我的浏览器工作每1.5秒就这样结束我能说“哦,孩子,我有一个我!就像一个真正的即时通讯客户!

2 个解决方案

#1


6  

Read about Comet

读到彗星

#2


0  

I've actually been working on a small .NET Web App that uses the Ajax with long polling technique described.

实际上,我正在开发一个小的。net Web应用程序,它使用了所描述的长轮询技术的Ajax。

Depending on what technology you're using, you could use thread signaling mechanisms to hold your request until an update is retrieved. With ASP.NET I'm running my server on a single machine, so I store a reference to my Producer object (which contains a thread that processes the data). To initiate the data pull, my service's Subscribe method is called, which creates a Consumer object that's registered with the Producer. If the Consumer is long polling mode, it has a AutoResetEvent which is signaled whenever it receives new data, and whenever the web client makes a request for data, the Consumer first waits on the reset event, and then returns it.

根据您正在使用的技术,您可以使用线程信号机制来保存您的请求,直到获取更新。ASP。我在一台机器上运行我的服务器,所以我存储对我的生产者对象的引用(它包含一个处理数据的线程)。为了启动数据提取,调用了我的服务的订阅方法,它创建了一个在生产者注册的消费者对象。如果使用者是长轮询模式,那么它有一个AutoResetEvent,每当它接收到新数据时,以及每当web客户端发出数据请求时,使用者首先等待重置事件,然后返回它。

But you're mentioning something about PHP - as far as I know persistence is maintained through serialization, not actually keeping the object in memory, so I don't know how you could reference a Producer object using $_CACHE[] or $_SESSION[]. When I developed in PHP I never really knew anything about multithreading so I didn't play around with it, but I guess you can look into that.

但是您提到了一些关于PHP的内容——据我所知,持久性是通过序列化来维护的,而不是将对象保存在内存中,因此我不知道如何使用$_CACHE[]或$_SESSION[]引用生成器对象。当我在PHP中开发时,我对多线程一无所知,所以我没有使用它,但是我想你们可以研究一下。

Using infinite loops is going to consume a lot of your processing power - I would exhaust all other options first.

使用无限循环将消耗大量的处理能力——我将首先耗尽所有其他选项。

#1


6  

Read about Comet

读到彗星

#2


0  

I've actually been working on a small .NET Web App that uses the Ajax with long polling technique described.

实际上,我正在开发一个小的。net Web应用程序,它使用了所描述的长轮询技术的Ajax。

Depending on what technology you're using, you could use thread signaling mechanisms to hold your request until an update is retrieved. With ASP.NET I'm running my server on a single machine, so I store a reference to my Producer object (which contains a thread that processes the data). To initiate the data pull, my service's Subscribe method is called, which creates a Consumer object that's registered with the Producer. If the Consumer is long polling mode, it has a AutoResetEvent which is signaled whenever it receives new data, and whenever the web client makes a request for data, the Consumer first waits on the reset event, and then returns it.

根据您正在使用的技术,您可以使用线程信号机制来保存您的请求,直到获取更新。ASP。我在一台机器上运行我的服务器,所以我存储对我的生产者对象的引用(它包含一个处理数据的线程)。为了启动数据提取,调用了我的服务的订阅方法,它创建了一个在生产者注册的消费者对象。如果使用者是长轮询模式,那么它有一个AutoResetEvent,每当它接收到新数据时,以及每当web客户端发出数据请求时,使用者首先等待重置事件,然后返回它。

But you're mentioning something about PHP - as far as I know persistence is maintained through serialization, not actually keeping the object in memory, so I don't know how you could reference a Producer object using $_CACHE[] or $_SESSION[]. When I developed in PHP I never really knew anything about multithreading so I didn't play around with it, but I guess you can look into that.

但是您提到了一些关于PHP的内容——据我所知,持久性是通过序列化来维护的,而不是将对象保存在内存中,因此我不知道如何使用$_CACHE[]或$_SESSION[]引用生成器对象。当我在PHP中开发时,我对多线程一无所知,所以我没有使用它,但是我想你们可以研究一下。

Using infinite loops is going to consume a lot of your processing power - I would exhaust all other options first.

使用无限循环将消耗大量的处理能力——我将首先耗尽所有其他选项。