如何在服务器端事件上注册客户端事件侦听器?

时间:2021-08-26 15:53:18

(Assuming it's even possible in .Net, of course.)

(当然,假设它甚至可以在.Net中使用。)

Ultimately, what I want to accomplish is an AJAX-based messaging system. I already have a database table for messages, and an ASPX to add new messages (and declare old messages no longer relevant).

最终,我想要实现的是基于AJAX的消息传递系统。我已经有了一个消息数据库表和一个ASPX来添加新消息(并声明旧消息不再相关)。

The current messaging system simply polls the server every 15 seconds, and re-pulls the current message set.

当前的消息传递系统每15秒轮询一次服务器,然后重新拉出当前的消息集。

What I am looking to do is: On $(document).ready(), register an ajax callback function that listens for a server-side event (e.g., MessagesUpdated) On table insert/update, fire MessagesUpdated server-side.

我要做的是:在$(document).ready()上,注册一个监听服务器端事件的ajax回调函数(例如,MessagesUpdated)在表插入/更新时,触发MessagesUpdated服务器端。

This way, whenever the table is updated (or new records added), any clients listening know that new data is available and can re-poll the server then.

这样,无论何时更新表(或添加新记录),任何侦听的客户端都知道新数据可用,然后可以重新轮询服务器。

Ideally, I'd also like to make the new data available as an event argument (to minimize re-polling the db).

理想情况下,我还希望将新数据作为事件参数提供(以最小化重新轮询数据库)。

I can find references to something like this in other languages, but I cannot find any actual code examples to get me started.

我可以在其他语言中找到类似这样的引用,但我找不到任何实际的代码示例来启动我。

Assuming this is possible to do via .Net, can anyone help get me started on this?

假设这可以通过.Net进行,任何人都可以帮我开始吗?

I'm using the 2.0 Framework. Also while I added a VB.Net tag, I can read C# reasonably well, so please feel free to post in either language.

我正在使用2.0 Framework。另外,当我添加一个VB.Net标签时,我可以很好地阅读C#,所以请随意用两种语言发布。

Thanks in advance!

提前致谢!

Pete

皮特

4 个解决方案

#1


3  

Take a look into long polling. Basically what it does is set a long timeout period on AJAX requests. The client then waits for the server to respond. This is much more efficient and instantaneous than sending requests every few seconds.

看看长期投票。基本上它的作用是在AJAX请求上设置一个很长的超时时间。然后客户端等待服务器响应。这比每隔几秒发送请求更有效和即时。

How to do a long polling client in C#?

如何在C#中建立长轮询客户端?

#2


1  

For modern browsers, you could use websockets to open a continuous connection to the server, through which the server could notify you of a server-side event. In browsers without websockets support, you would need to use long polling as mentioned in mrtsherman's answer.

对于现代浏览器,您可以使用websockets打开与服务器的连续连接,服务器可以通过该连接通知您服务器端事件。在没有支持websockets的浏览器中,您需要使用mrtsherman的答案中提到的长轮询。

#3


0  

HTML5 introduced websockets to address this issue, but support is sparse. If you used it you would need a fallback, which would probably be the long polling mrtsherman described.

HTML5引入了websockets来解决这个问题,但支持很少。如果您使用它,您将需要一个后备,这可能是长轮询mrtsherman描述。

#4


0  

I doubt it will work with .NET 2.0, but if you can work around that, you should check out SignalR. SignalR is a complete client- and server-side solution with JS on client and ASP.NET on the back end to create these kinds of applications. There's a good summary here.

我怀疑它是否适用于.NET 2.0,但是如果你可以解决这个问题,你应该查看SignalR。 SignalR是一个完整的客户端和服务器端解决方案,在客户端使用JS,在后端使用ASP.NET来创建这些类型的应用程序。这里有一个很好的总结。

Quoting directly from that blog post:

直接从该博客文章引用:

On the client

在客户端上

$(function () {
// Proxy created on the fly
var chat = $.connection.chat;

// Declare a function on the chat hub so the server can invoke it
chat.addMessage = function (message) {
    $('#messages').append('<li>' + message + '</li>');
};

$("#broadcast").click(function () {
    // Call the chat method on the server
    chat.send($('#msg').val());
});

// Start the connection
$.connection.hub.start();
});

And on the server

并在服务器上

public class Chat : Hub
{
    public void Send(string message)
    {
        // Call the addMessage method on all clients
        Clients.addMessage(message);
    }
}

#1


3  

Take a look into long polling. Basically what it does is set a long timeout period on AJAX requests. The client then waits for the server to respond. This is much more efficient and instantaneous than sending requests every few seconds.

看看长期投票。基本上它的作用是在AJAX请求上设置一个很长的超时时间。然后客户端等待服务器响应。这比每隔几秒发送请求更有效和即时。

How to do a long polling client in C#?

如何在C#中建立长轮询客户端?

#2


1  

For modern browsers, you could use websockets to open a continuous connection to the server, through which the server could notify you of a server-side event. In browsers without websockets support, you would need to use long polling as mentioned in mrtsherman's answer.

对于现代浏览器,您可以使用websockets打开与服务器的连续连接,服务器可以通过该连接通知您服务器端事件。在没有支持websockets的浏览器中,您需要使用mrtsherman的答案中提到的长轮询。

#3


0  

HTML5 introduced websockets to address this issue, but support is sparse. If you used it you would need a fallback, which would probably be the long polling mrtsherman described.

HTML5引入了websockets来解决这个问题,但支持很少。如果您使用它,您将需要一个后备,这可能是长轮询mrtsherman描述。

#4


0  

I doubt it will work with .NET 2.0, but if you can work around that, you should check out SignalR. SignalR is a complete client- and server-side solution with JS on client and ASP.NET on the back end to create these kinds of applications. There's a good summary here.

我怀疑它是否适用于.NET 2.0,但是如果你可以解决这个问题,你应该查看SignalR。 SignalR是一个完整的客户端和服务器端解决方案,在客户端使用JS,在后端使用ASP.NET来创建这些类型的应用程序。这里有一个很好的总结。

Quoting directly from that blog post:

直接从该博客文章引用:

On the client

在客户端上

$(function () {
// Proxy created on the fly
var chat = $.connection.chat;

// Declare a function on the chat hub so the server can invoke it
chat.addMessage = function (message) {
    $('#messages').append('<li>' + message + '</li>');
};

$("#broadcast").click(function () {
    // Call the chat method on the server
    chat.send($('#msg').val());
});

// Start the connection
$.connection.hub.start();
});

And on the server

并在服务器上

public class Chat : Hub
{
    public void Send(string message)
    {
        // Call the addMessage method on all clients
        Clients.addMessage(message);
    }
}