[译]SignalR将ISD应用推向新的高度
By robot-v1.0
本文链接 https://www.kyfws.com/applications/signalr-pushes-isd-applications-to-next-level-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2689 个词 阅读量 0[译]SignalR将ISD应用推向新的高度
原文地址:https://www.codeproject.com/Articles/717323/SignalR-Pushes-ISD-Applications-to-Next-Level
原文作者:dingjing
译文由本站 robot-v1.0 翻译
前言
ISD applications, web applications in general, are built on top of HTTP, which uses a pull-based communication mechanism. All communication requests are initiated or pulled from clients. Web servers can only respond to the requests. They cannot initiate or push communications
ISD应用程序(通常是Web应用程序)建立在HTTP之上,HTTP使用基于拉的通信机制.所有通信请求都从客户端发起或拉出. Web服务器只能响应请求.他们无法发起或推送通信 代码项目(CodeProject) ### 介绍(Introduction)
ISD应用程序(通常是Web应用程序)建立在HTTP之上,HTTP使用基于拉的通信机制.所有通信请求都从客户端发起或拉出. Web服务器只能响应请求.他们无法启动或将通信推送给客户端.换一种说法(简化). ISD应用程序是从客户端调用的服务器端方法的集合.在浏览器中键入URL时,您将其称为(ISD applications, web applications in general, are built on top of HTTP, which uses a pull-based communication mechanism. All communication requests are initiated or pulled from clients. Web servers can only respond to the requests. They cannot initiate or push communications to clients. Put it another (simplified) way. ISD applications are a collection of server-side methods to be called from client-side. When you type in a URL in a browser, you call its)Page_Load()(Page_Load())方法.当您单击按钮或在下拉列表中进行选择时,您会调用它们对应的(method. When you click a button or make a selection in a dropdown list, you call their corresponding)Button_Click()(Button_Click())要么(or)DDL_SelectedIndexChanged()(DDL_SelectedIndexChanged())方法.(methods.)
仅拉式通信的局限性使得在ISD应用程序中难以实现基于推式的功能.例如,如果多个用户尝试同时修改同一记录,则只有第一个用户才能保存其更改.当其他人尝试保存所做的更改时,会弹出消息"记录已更改.请刷新".如果在第一个用户保存更改后,服务器立即向所有其他用户发出通知,以免他们浪费不必要的时间或精力,那就更好了.(The limitation of pull-only communication makes it difficult to implement push-based functionality in ISD applications. For example, if several users try to modify the same record at the same time, only the first user can save his changes. When the others try to save their changes, they will be greeted with a popup message, “The record has been changed. Please refresh.” It would be much better if, after the first user saves his changes, the server immediately pushes out a notification to all other users, so that they don’t waste any time or effort unnecessarily.)### 解(Solution)
可以在当前的仅拉框架内模拟此类推送功能.例如,将Timer控件嵌入到EditRecord页面中,并定期检查记录的时间戳或校验和.但是,由于重复的不必要的Web和数据库流量,实现效率极低.理想的解决方案是在第一个用户保存更改后立即从服务器端调用客户端方法.我不是在谈论使用(It is possible to mimic such push functionality within the current pull-only framework. For example, embed a Timer control in the EditRecord page, and check the record’s timestamp or check-sum periodically. However, the implementation is extremely inefficient because of repeated unnecessary web and database traffics. An ideal solution is to call a client-side method from server-side as soon as the first user saves his changes. I am not talking about using)RegisterStartupScript()(RegisterStartupScript())注入JavaScript代码,该代码仅在发起请求的单个客户端上执行.我建议从服务器端在所有客户端上调用JavaScript方法.不可能?(to inject JavaScript code, which is executed only at the single client that initiates the request. What I suggest is to call a JavaScript method on ALL clients from server-side. Impossible?) 信号R(SignalR) 使我能够做到这一点.(enables me to do just that.)
SignalR是一个ASP.NET库,它允许服务器与客户端之间进行双向通信.换句话说,它允许Web应用程序通过从服务器调用客户端代码将内容推送到客户端,反之亦然.在下面的演示项目中,我将向您展示在ISD应用程序中实现推送通知有多么简单.(SignalR is an ASP.NET library which allows bi-directional communication between server and client. In other words, it allows web applications to push content to clients by calling client code from servers, and vice versa. In the following demo project, I will show you how simple it is to implement a push notification in an ISD application.)### 实作(Implementation)
步骤1:使用所需的配置构建常规的ISD应用程序(Step 1: Build a regular ISD application with required configuration)
SignalR 2.0在系统配置上非常挑剔.检查一下(SignalR 2.0 is pretty picky on system configuration. Check) 它的要求页面(its requirement page) 确保您的服务器受支持,并且您的目标用户具有兼容的浏览器.为了创建演示应用程序,我使用了以下配置:(to make sure your server is supported, and your intended users have compatible browsers. To create the demo application, I used the following configurations:)
- ISD版本10.2.1(ISD v10.2.1)
- VS 2012(VS 2012)
- .NET 4.5(.NET 4.5)
- 网站(Web site)
- C#(C#)
- 在Windows 7下开发(Develop under Windows 7)
- 部署到Windows Azure(Deploy to Windows Azure)
- IIS Express(IIS Express) C#网站只是我自己的喜好. VB Web应用程序也应该工作.(C# web site is just my own preference. VB web application should also work.)#### 步骤2:添加SignalR库(Step 2: Add SignalR library)
在Visual Studio中打开应用程序,通过NuGet包管理添加SignalR库.(Open the application in Visual Studio, add SignalR library via NuGet package management.)
步骤3:在服务器中创建中心(Step 3: Create a hub in server)
在App_Code \ Shared文件夹中添加以下2个类.(Add the following 2 classes in App_Code\Shared folder.)
步骤4:在客户端中定义通知方法(Step 4: Define notification method in client)
在ISD中,将以下JavaScript代码添加到EditRecord页面的序言中.(In ISD, add the following JavaScript code to the EditRecord page’s prologue.)
步骤5:来自服务器的呼叫通知(Step 5: Call notification from server)
推送通知的最佳时间是在保存更改之后,即在数据库中成功提交事务之后.在Visual Studio中,覆盖(The best time to push the notification is after changes have been saved, i.e. after the transaction is committed successfully in database. In Visual Studio, override)CommitTransaction()(CommitTransaction())在EditRecord的页面类中.(in EditRecord’s page class.)
演示页面(Demo page)
只需添加推送通知即可.请点击(That’s all to add the push notification. Click) 这里(here) 打开演示页面.在2个或更多不同的浏览器中打开它.在一个浏览器中单击"保存"按钮,所有其他浏览器都会弹出通知.当然,还有改进的空间.例如,(to open a demo page. Open it in 2 or more different browsers. Click “Save” button in one browser, and all other browsers will popup the notification. Of course, there are rooms for improvement. For example,)
- 使用侵入性较小的通知,例如(Use a less invasive notification, e.g.) 烤面包机(toastr) 而不是警报.(instead of alert.)
- 发送记录ID作为通知方法中的参数.仅在ID与编辑中的当前ID匹配时显示通知.(Send record ID as a parameter in the notification method. Show notification only if the ID matches the current one in editing.)
结论(Conclusion)
SignalR使Web应用程序能够将内容从服务器推送到客户端.服务器与客户端通信中的这一新维度可以将您的ISD应用程序推向新的高度.(SignalR enables web applications to push content from server to client. This new dimension in server-client communication can push your ISD applications to the next level.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C++ Windows 新闻 翻译