Signalr with ASP.Net part2

17
SignalR By Esraa Ammar

Transcript of Signalr with ASP.Net part2

Page 1: Signalr with ASP.Net part2

SignalR

B y E s r a a A m m a r

Page 2: Signalr with ASP.Net part2

Topics Focused On!!!• HubName Attribute

• HubMethodName Attribute

• Strongly-Typed Hubs

• Synchronous – Asynchronous Method

• Complex object

• Information about the client

• Selecting which clients will receive the RPC

Page 3: Signalr with ASP.Net part2

HubName AttributeIf you want to specify a different Hub name for clients to use, add the HubName attribute.

Page 4: Signalr with ASP.Net part2

HubMethodName AttributeIf you want to specify a different Hub method name for clients to use, add the HubMethodName attribute.

Page 5: Signalr with ASP.Net part2

Strongly-Typed HubsTo define an interface for your hub methods that your client can reference.

Page 6: Signalr with ASP.Net part2

Synchronous – AsynchronousMethod

Making a Hub method asynchronous avoids blocking the connection when it uses the WebSocket transport. When a Hub method executes synchronously and the transport is WebSocket, subsequent invocations of methods on the Hub from the same client are blocked until the Hub method completes.

Page 7: Signalr with ASP.Net part2

Write async method• Server:

Page 8: Signalr with ASP.Net part2

Demo

Page 9: Signalr with ASP.Net part2

Complex object

Page 10: Signalr with ASP.Net part2

Information about the client• The connection ID of the calling client.

– string connectionID = Context.ConnectionId;

The connection ID is a GUID that is assigned by SignalR (you can't specify the value in your own code). There is one connection ID for each connection, and the same connection ID is used by all Hubs if you have multiple Hubs in your application.

Page 11: Signalr with ASP.Net part2

QueryString• Query string data.

– In Server• Microsoft.AspNet.SignalR.Hosting.INameValueCollection queryString =

Context.Request.QueryString;• string parameterValue = queryString["parametername"]

– In Client• $.connection.hub.qs = { username' : ‘value' };

• You can add query string parameters in the client by configuring the connection, which is a convenient way to pass data about the client from the client to the server.

Page 12: Signalr with ASP.Net part2

Demo

Page 13: Signalr with ASP.Net part2

Selecting which clients will receive the RPC 1

• All connected clients.– Clients.All.ViewMessage(name, message);

• Only the calling client.– Clients.Caller.ViewMessage (name, message);

• All clients except the calling client.– Clients.Others.ViewMessage(name, message);

• A specific client identified by connection ID.– Clients.Client(ConnectionId).ViewMessage(name, message);

Page 14: Signalr with ASP.Net part2

Selecting which clients will receive the RPC 2

• All connected clients except the specified clients, identified by connection ID.– Clients.AllExcept(connectionId1,

connectionId2).ViewMessage(name, message);

• All connected clients in a specified group.– Clients.Group(groupName).ViewMessage(name, message);

• All connected clients in a specified group except the specified clients, identified by connection ID.– Clients.Group(groupName, connectionId1, connectionId2).

ViewMessage(name, message);

Page 15: Signalr with ASP.Net part2

Selecting which clients will receive the RPC 3

• All connected clients in a specified group except the calling client.– Clients.OthersInGroup(groupName).ViewMessage(name,

message);

• All clients and groups in a list of connection IDs.– Clients.Clients(ConnectionIds).ViewMessage(name,

message);

• A list of groups.– Clients.Groups(GroupIds).ViewMessage(name,

message);

Page 16: Signalr with ASP.Net part2

Demo

Page 17: Signalr with ASP.Net part2

Thanks