简体   繁体   English

ASP.NET MVC - 将服务器端事件传播到客户端

[英]ASP.NET MVC - Propagate a server side event to the client

In my ASP.NET MVC app, the user clicks a button on the UI to make a phone call. 在我的ASP.NET MVC应用程序中,用户单击UI上的按钮进行电话呼叫。 An ajax request goes to the MVC app, which calls a phone dialer -- a method in library that calls an external component to make a call. ajax请求转到MVC应用程序,该应用程序调用电话拨号程序 - 库中的一种方法,用于调用外部组件进行调用。

If a dialed call is terminated by the recipient of the call, the phone dialer component raises an event by calling an event handler in its own class. 如果拨打的呼叫被呼叫的接收者终止,则电话拨号器组件通过调用其自己的类中的事件处理程序来引发事件。

I want to propagate that event to the client side so that it may update its UI. 我想将该事件传播到客户端,以便它可以更新其UI。

An Option I Can't Use 我不能使用的选项

I've looked at JavaScript Server-sent events. 我查看了JavaScript Server发送的事件。 However, they are different from my situation in the way that in a JavaScript Server-sent event, here's what happens: 但是,它们与我在JavaScript Server发送的事件中的情况不同,以下是发生的情况:

1) The client initiates a connection on a new socket to the server. 1)客户端在服务器的新套接字上启动连接。 The key difference being, the client initiates the connection. 关键区别在于,客户端启动连接。

2) The connection is held live and active until the server or the client want to terminate it. 2)连接保持活动并处于活动状态,直到服务器或客户端想要终止它。

3) The server has to be alive all throughout the time from the time the connection is made until the client or the server want to terminate the connection and no longer exchange notifications. 3)从建立连接到客户端或服务器想要终止连接并且不再交换通知的整个过程中,服务器必须一直处于活动状态。 This means that a new socket connection and consequently a new worker thread to service the notification exchange is used per client. 这意味着每个客户端使用一个新的套接字连接,从而使用一个新的工作线程来为通知交换提供服务。

If I use server-sent events, I will have to make a server that stays alive. 如果我使用服务器发送的事件,我将不得不使服务器保持活跃状态​​。 That means I will have to have a new action on a controller and a corresponding view that gets called at the very beginning and stays alive until the notification about the call hang-up is received. 这意味着我必须对控制器和相应的视图进行新操作,该视图在开始时被调用并保持活动状态,直到收到有关呼叫挂起的通知为止。

This can not only be expensive, it is also counter-intuitive to my design as I do not want to be redirected to a new View just to listen to events. 这不仅昂贵,而且对我的设计也是违反直觉的,因为我不想被重定向到新视图只是为了听事件。

Anyone have any other alternative? 有没有其他选择?

You have to either use a WebSocket or Long polling . 您必须使用WebSocket长轮询 These both require you to set up a connection from the client to the server, additional to the normal HTTP cycle. 这些都要求您设置从客户端到服务器的连接,这是正常HTTP周期的补充。 And what else would you expect? 你还有什么期望? When the page is sent, the communication between client and server is done. 发送页面时,客户端和服务器之间的通信完成。 The HTTP cycle is over, no more data can float through. HTTP循环结束,不再有数据可以浮动。 The new connection needs to originate from the client because the client does not allow arbitrary incoming connections. 新连接需要来自客户端,因为客户端不允许任意传入连接。

I do not think there are other alternatives in normal case. 我不认为在正常情况下还有其他选择。

SignalR, etc, all require connection to be alive or periodically restarted by client. SignalR等都要求连接处于活动状态或由客户端定期重启。 I am not aware of anything that allows server to initiate connection with a browser (it does not even seem technically possible due to proxies/firewalls etc). 我不知道任何允许服务器启动与浏览器连接的东西(由于代理/防火墙等,它在技术上似乎不可能)。

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

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