将通知推送到 JavaScript?

发布于 2024-09-05 09:58:53 字数 415 浏览 4 评论 0原文

我只是想知道是否有办法让服务器将信息推送到 JavaScript 函数。本质上我有一个仪表板类型的页面,它有一个 javaScript 函数来从服务器获取更新并更新仪表板。

我希望我的服务器能够“ping”JS。

我什至不知道这怎么可能(我猜 Twitter 和 Facebook 使用投票?),但我想我应该问一下。

我听说过 Comet,但我不知道这是否适用于普通的标准 IIS 7 安装? (如果它以任何方式重要的话,它是一个 SharePoint 2010 网站)如果我理解正确的话,Comet 本质上是一个持续打开的连接,所以看起来它实际上与我想要的相反(减少请求数,从而减少负载)

I'm just wondering if there is a way to have a server push information to a JavaScript function. Essentially I have a Dashboard-type page that has a javaScript function to get updates from the server and update the dashboard.

I would like my server to be able to "ping" the JS.

I don't even know how that could be possible (I'm guessing Twitter and Facebook use polling?), but I'd thought I ask.

I heard of Comet, but I don't know if that works with a plain standard IIS 7 installation? (It's a SharePoint 2010 site if that matters in any way) If I understand it correctly, Comet is essentially a constantly open connection, so it seems like it's actually the opposite of what I want (reducing # of requests and therefore load)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

自控 2024-09-12 09:58:53

如果您正在寻找适用于 IIS 的 Comet 服务器,请查看 WebSync;正是如此:)

If you're looking for a comet server for IIS, check out WebSync; it's exactly that :)

小忆控 2024-09-12 09:58:53

使用 HTTP 不可能真正从服务器发起连接。 Comet 实际上并不是一种单一技术,而是一组不同的解决方法(其中许多方法在您链接的文章中进行了描述)。

有关 IIS 中的 Comet 技术的信息,请参阅上一个问题IIS 中的 Comet 编程。 WebSync 是其中讨论的程序之一。

Truly initiating a connection from the server is not possible using HTTP. Comet isn't really a single technique, but a set of different workarounds (many of which are described at the article you linked).

For information on Comet techniques with IIS, see the prior question, Comet Programming in IIS. One of the programs discussed there is WebSync.

虫児飞 2024-09-12 09:58:53

Comet 风格的解决方法是获得此功能的最常见方法。连接不是一直打开的,而是被限制为每 x 秒进行一次调用,然后在超时时重试。超时本质上意味着服务器在轮询期间没有任何内容可以提供给客户端。您将看到 Etherpad 代码使用了相同的方法,该方法现已集成到其他 Google 产品中,例如 Google Docs 和 Wave。

A Comet style workaround is the most common way to get this functionality. The connection is not constantly open, but rather throttled to make calls every x seconds, then try again upon timeout. The timeout essentially means that the server didn't have anything to give to the client in the duration of the poll. You'll see that the Etherpad code used this same approach, which has been integrated into other Google products now like Google Docs and Wave.

感情洁癖 2024-09-12 09:58:53

正如 Samuel Neff 所说,“您将需要一个开放连接来将数据从服务器“推送”到客户端。”

您可以使用 pubnub 之类的服务从客户端打开持久连接并支持旧版浏览器的回退。

我做了一个小演示来向您展示该应用程序的前端如何工作。该演示显示了 PubNub 延迟随时间的变化。源代码可在此处获取。

PubNub Latency Demo

浏览器订阅频道并在收到消息时触发回调。

 pubnub.subscribe({
     channel: 'my_channel',
     message: function(m){console.log(m)}
 });

在演示中,客户端还发布消息。在您的情况下,您将包含 PubNub IIS 库

pubnub.Subscribe<string>(channel="mychannel", DisplaySubscribeReturnMessage,    DisplaySubscribeConnectStatusMessage, DisplayErrorMessage);
// NOTE: DisplaySubscribeReturnMessage, DisplaySubscribeConnectStatusMessage and DisplayErrorMessage are callback methods

As Samuel Neff says, "You're going to need an open connect to "push" data from the server to the client."

You can use a service like pubnub to open persistent connections from the client and support fallbacks for older browsers.

I made a small demo to show you how the front-end of this application may work. The demo shows PubNub latency over time. The source is available here.

PubNub Latency Demo

The browser subscribes to a channel and fires a callback when a message is received.

 pubnub.subscribe({
     channel: 'my_channel',
     message: function(m){console.log(m)}
 });

In the demo the client also publishes messages. In your case you would include the PubNub IIS library.

pubnub.Subscribe<string>(channel="mychannel", DisplaySubscribeReturnMessage,    DisplaySubscribeConnectStatusMessage, DisplayErrorMessage);
// NOTE: DisplaySubscribeReturnMessage, DisplaySubscribeConnectStatusMessage and DisplayErrorMessage are callback methods
苦笑流年记忆 2024-09-12 09:58:53

您将需要一个开放连接来将数据从服务器“推送”到客户端。因此,即使您使用 Flash 之类的插件来打开支持双向通信的套接字连接,您也有一个打开的套接字连接。

您的声明“减少请求数并因此减少负载”确实有问题。您将请求数量与负载等同起来,这是不准确的。对于 Comet,大多数请求都在等待数据。因此,您可以有非常多的请求,但服务器上的负载实际上非常低——除了工作线程池中的等待线程之外,它几乎不使用资源。

使用彗星。效果很好,易于实施,并且完全满足您的需要。

You're going to need an open connect to "push" data from the server to the client. So even if you went the route of using a plugin like Flash to open a socket connection which supports two-way communications, you have an open socket connection.

Your statement "reducing # of requests and therefore load" really is problematic. You're equating number of requests with load and that is not accurate. With Comet the majority of requests are waiting on data. Therefore you can have a very high number of requests, but really a very low load on the server--it's hardly using resources besides a waiting thread from the worker thread pool.

Use Comet. Works great, is simple to implement, and does exactly what you need.

瘫痪情歌 2024-09-12 09:58:53

您必须以相反的方式进行操作,即让客户端使用 JS“ping”服务器。

您可以执行以下操作:

function pollServer()
    {
    // Get some parameter
    var param = .......
    AJAXCall("page.php?param="+param, onReturn);
    }

function onReturn(response)
    {
    // do something with response
    setTimeout("pollServer()", 5000);
    }

pollServer();

AJAXCall 是您用来执行 AJAX 调用的函数,并在收到响应时调用 onReturn
在这种情况下,一旦收到响应,它就会等待 5 秒,然后再次轮询服务器

You have to do it the other way around, by having the client "pinging" the server with JS.

You can do something like:

function pollServer()
    {
    // Get some parameter
    var param = .......
    AJAXCall("page.php?param="+param, onReturn);
    }

function onReturn(response)
    {
    // do something with response
    setTimeout("pollServer()", 5000);
    }

pollServer();

AJAXCall being the function you use to do an AJAX call and that calls onReturn when it gets a response.
Once it gets a response it waits in this case 5 seconds and polls the server again

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文