首页 > > > Syncfusion.ASP.NET SignalR.2013.pdf

Syncfusion.ASP.NET SignalR.2013.pdf

Syncfusion.ASP.NET SignalR.2013…

上传者: firmwu 2013-03-30 评分1 评论0 下载54 收藏10 阅读量1038 暂无简介 简介 举报

简介:本文档为《Syncfusion.ASP.NET SignalR.2013pdf》,可适用于软件工程领域,主题内容包含ASPNETSignalRIncrediblysimplerealtimefeaturesforyourwebappsJoseMAguilarASP符等。

ASP.NET SignalR Incredibly simple real-time features for your web apps Jose M. Aguilar ASP.NET SIGNALR - INCREDIBLY SIMPLE REAL-TIME FEATURES FOR YOUR WEB APPS Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and Krasis Consulting S.L., nor its dealers or distributors, will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein. Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark. Krasis Consulting, S. L. 2013 www.campusmvp.net ALL RIGHTS RESERVED. NO PART OF THIS BOOK MAY BE REPRODUCED, IN ANY FORM OR BY ANY MEANS, WITHOUT PERMISSION IN WRITING FROM THE PUBLISHER. ISBN: 978-84-939659-7-6 iii Contents CONTENTS ................................................................................................................ III 1. INTRODUCTION .................................................................................................... 7 1.- Internet, Asynchrony, Multiuser… wow! ...............................................................................7 2. HTTP: YOU ARE THE CLIENT, YOU ARE THE BOSS ................................... 9 1.- Http operation ..............................................................................................................................9 2.- Polling: The answer?.................................................................................................................. 10 3.- Push: The server takes the initiative ..................................................................................... 12 3.1.- Websockets .................................................................................................................... 13 3.2.- Server-Sent Events (a.k.a. API Event Source) ......................................................... 14 3.3.- Push today ....................................................................................................................... 16 3.3.1.- Long polling ............................................................................................................. 16 3.3.2.- Forever frame ......................................................................................................... 17 3.4.- The world needs more than just Push ..................................................................... 18 3. SIGNALR ................................................................................................................. 21 1.- Introducing SignalR .................................................................................................................... 21 2.- What does SignalR offer?......................................................................................................... 22 3.- Installing SignalR ......................................................................................................................... 23 4.- Two levels of abstraction ........................................................................................................ 24 4. PERSISTENT CONNECTIONS........................................................................... 27 1.- Introduction ................................................................................................................................ 27 2.- Implementation on the server side ....................................................................................... 28 2.1.- Events of a Persistent Connection ............................................................................ 29 2.2.- Sending messages to clients ........................................................................................ 31 2.3.- Asynchronous event processing ................................................................................ 33 2.4.- Connection groups ........................................................................................................ 34 3.- Client-side implementation ..................................................................................................... 35 3.1.- Initiating the connection using the Javascript client .............................................. 36 3.2.- Support for cross-domain connections.................................................................... 38 3.3.- Sending messages ........................................................................................................... 38 3.4.- Receiving messages ........................................................................................................ 40 3.5.- Other events available at the client .......................................................................... 41 4.- Transport negotiation .............................................................................................................. 41 5.- Complete example: Tracking visitors ................................................................................... 42 5.1.- Implementation on the client side ............................................................................. 43 5.1.1.- HTML markup ........................................................................................................ 43 iv ASP.NET SignalR Incredibly simple real-time features for your web apps 5.1.2.- Scripts (Scripts/Tracking.js) ................................................................................. 44 5.2.- Implementation on the server side ........................................................................... 45 5.2.1.- Persistent connection (TrackerConnection.cs) ............................................. 45 5.2.2.- Startup code (Global.asax.cs) ............................................................................. 45 5. HUBS ....................................................................................................................... 47 1.- Introduction ................................................................................................................................ 47 2.- Server implementation ............................................................................................................. 48 2.1.- Route registration.......................................................................................................... 48 2.2.- Creating Hubs ................................................................................................................. 49 2.3.- Receiving messages ........................................................................................................ 49 2.4.- Sending messages to clients ........................................................................................ 52 2.5.- State maintenance .......................................................................................................... 54 2.6.- Accessing information about the request context ................................................ 55 2.7.- Notification of connections and disconnections.................................................... 56 2.8.- Managing groups ............................................................................................................. 56 3.- Client implementation .............................................................................................................. 57 3.1 JavaScript clients ............................................................................................................. 57 3.2 Generating the proxy ................................................................................................... 57 3.3 Establishing the connection ......................................................................................... 58 3.4 Sending messages to the server ................................................................................. 59 3.5 Receiving messages at the server .............................................................................. 61 3.6 Logging .............................................................................................................................. 63 3.7 State maintenance .......................................................................................................... 64 3.8 Implementing the client without a proxy ................................................................ 65 4.- Complete example: Shared whiteboard .............................................................................. 67 4.1 Implementation on the client side ............................................................................. 67 4.1.1 HTML markup ........................................................................................................ 67 4.1.2 Scripts (Scripts/DrawingBoard.js) ..................................................................... 68 4.2 Implementation on the server side ........................................................................... 69 4.2.1 Hub (DrawingBoard.cs) ........................................................................................ 69 4.2.2 Startup code (Global.asax.cs) ............................................................................. 70 6. PERSISTENT CONNECTIONS AND HUBS FROM OTHER PROCESSES 71 1.- Access from other processes ................................................................................................. 71 2.- Using persistent connections.................................................................................................. 72 3.- Using hubs ................................................................................................................................... 72 4.- Complete example: progress bar .......................................................................................... 73 4.1.- Implementation on the client side ............................................................................. 74 4.1.1.- HTML markup ........................................................................................................ 74 4.1.2.- Styles (Styles/ProgressBar.css) .......................................................................... 74 4.1.3.- Scripts (Scripts/ProgressBar.js) ......................................................................... 75 4.2.- Implementation on the server side ........................................................................... 75 4.2.1.- Hub ............................................................................................................................ 75 4.2.2.- Expensive process (HardProcess.Aspx) ........................................................... 75 4.2.3.- Startup code (Global.asax.cs) ............................................................................. 76 Contents v 7. SIGNALR OUTSIDE THE WEB .......................................................................... 77 1.- SignalR clients ............................................................................................................................. 77 2.- Accessing services from non-web clients ............................................................................ 77 3.- Hosting services outside asp.net ........................................................................................... 79 <coder> Premium coached online training for busy developers More than canned videos Tutored by the ones who know most Specific training methodology Direct contact with our Students Office 91% of our students give us an A www.campusmvp.net Got no time and need to learn new programming skills? 7 CHAPTER 1.- INTERNET, ASYNCHRONY, MULTIUSER… WOW! An application that combines internet, asynchrony, and multiple users cooperating and interacting at the same time always deserves a “wow!”. At some point, we have all doubtlessly been amazed by the interactivity which some modern web systems can offer, such as Facebook, Twitter, Gmail, Google Docs or many others, where we receive updates almost in real time, without having to reload the page. For instance, when we are editing a document online using Google Docs and another user also accesses it, we can see that they have entered and follow the changes that they are making on the document. Even in a more everyday scenario such as a simple web chat, the messages being typed by our friend just appear as if by magic. Both systems use the same type of solution: asynchronous data transfer between the server and the clients in real time. Throughout these pages, we will learn how to implement impressive features of this kind using SignalR, a framework which will facilitate our task to the point of making it trivial. For this, we will first present a brief review of the problems that we find when developing real-time multiuser applications. We will quickly look at HTTP operation and its limitations for supporting this type of systems, and we will introduce the Push concept. We will also describe the standards that are being prepared by W3C and IETF, as well as techniques that we can currently use for implementing Push on HTTP. Next, we will introduce SignalR, describing its main features and the different levels of abstraction which it allows over the underlying protocols. We will implement different examples which will help us understand its bases and give us insight on how we can use this framework in our projects. Introduction 1 8 ASP.NET SignalR - Incredibly simple real-time features for your web apps Finally, we will describe how SignalR is independent of Web environments, which means that it can be hosted in any type of application and its real-time services can be consumed from practically any type of system. 9 CHAPTER 1.- HTTP OPERATION HTTP (HyperText Transfer Protocol) is the language in which the client and the server of a web application speak to each other. It was initially defined in 19961, and the simplicity and versatility of its design are, to an extent, responsible for the success and expansion of the Web and the Internet as a whole. Its operation is based on a request-response schema which is always started by the client. This procedure is often referred to as the Pull model: When a client needs to access a resource hosted by a server, it purposely initiates a connection to it and requests the desired information using the “language” defined by the HTTP protocol. The server processes this request, returns the resource that was asked for (which may be the contents of an existing file or the result of running a process) and the connection is instantly closed. If the client needs to obtain a new resource, the process starts again from the beginning: A connection to the server is opened, the request for the resource is sent, the server processes it, it returns the result and then closes the connection. This happens every time we access a webpage, images or other resources that are downloaded by the browser, to give a few examples. Figure 1. HTTP communication between a browser and a web server 1 Specification of HTTP 1.0: http://www.w3.org/Protocols/HTTP/1.0/spec.html HTTP: You are the client, you are the boss 2 10 ASP.NET SignalR - Incredibly simple real-time features for your web apps As you can guess by looking at the image above, it is a synchronous process: After sending the request to the server, the client is left to wait, doing nothing until the response is available. Although this operation is a classic in web systems, the HTTP protocol itself can support the needs for asynchrony of modern applications, owing to the techniques generally known as AJAX (Asynchronous JavaScript And XML). Using AJAX techniques, the exchange of information between the client and the server can be done without leaving the current page. At any given moment, the client may initiate a connection to the server using JavaScript, request a resource and process it (for example, updating part of the page). What is truly advantageous and has contributed to the emergence of very dynamic and interactive services, such as Facebook or Gmail, is that these operations are carried out asynchronously, that is, the user may keep using the system while the latter communicates with the server in the background to send or receive information. Figure 2. Ajax in a webpage This operating schema continues to use and abide by the HTTP protocol and the client-driven request/response model. The client is always the one to take the initiative, deciding when to connect to the server. However, there are scenarios where HTTP is not very efficient. With this protocol it is not easy to implement instant-messaging applications or chat-rooms, collaboration tools, multiuser online games or real-time information services, even when using asynchrony. The reason is simple: HTTP is not oriented to real time. There are other protocols, such as the popular IRC, which are indeed focused on achieving swifter communication in order to offer more dynamic and interactive services than the ones we can obtain using Pull. In them, the server can take the initiative and send information to the client at any time, without waiting for it to request it expressly. 2.- POLLING: THE ANSWER? As web developers, when we face a scenario where we need the server to be the one sending information to the client on its own initiative, the first solution that intuitively HTTP: You are the client, you are the boss 11 comes to our minds is to use the technique known as Polling. Polling basically consists in making periodic connections from the client to check whether there is any relevant update at the server. Figure 3. Polling in a chat room service The main advantages of this solution are, first, its easy implementation, and second, its universal application: it works in every case, with all browsers and with all servers, since it does nothing more than use the standard features of HTTP. And, of course, we still use the Pull model. However, sometimes the price of polling is too high. Constant connections and disconnections have a high cost in terms of bandwidth and processing at both ends of communication. And the worst part is that this cost increases proportionally to our need for faster updates. In an application providing real-time updates, it is easy to imagine the load that a server has to bear when it has thousands of users connected requesting several updates per second. There are techniques to mitigate these problems insofar as possible. One of them is to use adaptive periodicity, so that the interval between queries regularly adapts to the current system load or to the probability of new updates. It is quite easy to implement and can significantly improve resource consumption in some scenarios. There is a more conservative variant of polling, but it degrades user experience. It is the technique called piggy backing, which consists in not making del

该用户的其他资料

  • 名称/格式
  • 评分
  • 下载次数
  • 资料大小
  • 上传时间

用户评论

0/200
    暂无评论
上传我的资料

相关资料

资料评价:

/ 83
所需积分:1 立即下载
返回
顶部
举报
资料
关闭

温馨提示

感谢您对爱问共享资料的支持,精彩活动将尽快为您呈现,敬请期待!