Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
signalr / SignalR Programming in Microsoft ASP.NET.pdf
Скачиваний:
65
Добавлен:
25.05.2015
Размер:
19.23 Mб
Скачать

Installing SignalR

The easiest way to include SignalR in a project is by using the NuGet package manager. This great tool makes it easy to carry out the formerly lengthy process of downloading the components, copying the binaries to the project, and adding the references.

SignalR is distributed in diverse packages, where we can find server-side components, satellite assemblies with resources localized to different cultures, and various client implementations of these types of services.

It is possible to get the official packages available by using the graphic interface or entering the following command into the package management console7:

PM> Get-Package microsoft.aspnet.signalr -ListAvailable

Thus, in a web application, we will normally install the Microsoft.AspNet.SignalR package, which includes both server components and client libraries based on JavaScript. If we want to consume the services from any type of .NET application (including WinRT, Windows Phone 8, and Silverlight 5), we must install only the Microsoft.AspNet.SignalR.Client package on it.

PM> Install-package Microsoft.aspnet.signalr

It is also possible to create SignalR components directly from the development environment. In Visual Studio 2012, as long as we have installed the “ASP.NET and Web Frameworks 2012.2” update or a later one, it is possible to add components of this framework, as shown in Figure 3-7.

FIGURE 3-7  Adding SignalR components to a Visual Studio 2012 project.

Visual Studio 2013 includes out-of-the-box SignalR templates, so we can also add components of the main available versions directly, as shown in Figure 3-8.

7 You can access the Package Manager Console in Visual Studio via “Tools > Library Package Manager > Package Manager Console”.

Introducing SignalRChapter 3

25

www.it-ebooks.info

FIGURE 3-8  Adding SignalR components to a Visual Studio 2013 project.

In both cases, when we add these elements to our project, the necessary libraries will be downloaded and installed, and also some basic code with which to work will be included.

26 Chapter 3Introducing SignalR

www.it-ebooks.info

C H A P T E R 4

Persistent connections

The lower-level API with which we can approach the persistent connection that SignalR offers is illustrated in Figure 4-1. This API provides us with a layer of abstraction that isolates us from the

complexities inherent to keeping a permanent connection open between the client and the server and from the transports used to send information between both ends.

FIGURE 4-1  Abstraction levels used by SignalR.

In practice, this API gives us access to the communication channel that is quite similar to the one used traditionally when working at a low abstraction level with sockets: On the server side, we can be notified when connections are opened and closed and when data are received, as well as sending information to clients. On the client side, we can open and close connections, as well as sending and receiving arbitrary data. Also, just like with sockets, messages have no format; that is, they are raw data—normally text strings—that we will have to know how to interpret correctly at both ends.

From the point of view of the client, its operation is very easy. We just have to initiate a connection to the server, and we will be able to use it to send data right away. We will perform the reception of information using a callback function that is invoked by the framework after its reception.

The server side is not very complex either. Take a look at Figure 4-2. Persistent connections are classes that inherit from PersistentConnection and override some of the methods that allow taking control when a relevant event occurs, such as the connection or disconnection of new clients or the reception of data. From any of them, we will be able to send information to the client that has caused the event, to all clients connected to the service, or to a group of them.

27

www.it-ebooks.info

Соседние файлы в папке signalr