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

var message = "Welcome, " + username + "!"; return Connection.Send(connectionId, message);

}

Another possibility would be to use the query string to send information. For this, the SignalR client allows us to specify an additional parameter at the point at which the connection is defined. In this parameter, we can add key-value mappings, either in the form of a string or an object, which will be annexed to all the requests made to the SignalR server:

// Client side:

var name = prompt("Your username"); var conn = $.connection(

"/realtime/chat",

"username="+name // or { username: name }

);

...

conn.start();

// Server side:

protected override Task OnConnected(IRequest request, string connectionId)

{

var userName = request.QueryString["username"] ?? connectionId; var message = "Welcome, " + userName + "!";

return Connection.Send(connectionId, message);

}

Other events available at the client

The connection object has a large number of events that allow us to take control at certain moments in the life cycle of the connection if we register the callback methods that we want to be invoked when the time comes. The most interesting ones are the following:

■■received() and error(), which we already saw and which allow us to specify the function to be executed when data are received or when an error occurs in the connection.

■■connectionSlow(), which allows entering logic when the connection is detected to be slow or unstable.

■■stateChanged(), invoked when the state of the connection changes.

■■reconnected(), when there is a reconnection of the client after its connection has been closed due to time-out or any other cause.

In the SignalR repository in GitHub7, you can find the documentation about methods, events, and properties offered by the JavaScript client connections.

7 Javascript client documentation: https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client

Persistent connectionsChapter 4

47

www.it-ebooks.info

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