A, SignalR Reviews

First, the communication mode

In the past, we chat message mode (transmission message or notification monomer) - Friend (one to one), the group (one to many), to facilitate the management Friend - grouping.

So chat and buddy management reference qq, refreshing packets labels page will load from the new list of groups within groups and buddy lists,

Refresh Group tab page will load from the new group list, click the friends chat messages from the new database inside the new acquisition (including read and unread, history, in fact, read the latest news - do not read and unread, says ).

Stateless requests, we do not know if I have to send a message, we need to have real-time monitor is no news, past ajax poll, today's signalr.

Two, SignalR real-time mode

After the user signalr connection to the server will be a real-time connection, and save the user ID, then we can if we have real-time alerts if new messages and monitor user sends a message.

Specifically:

Caption: 1, in the front end (the specified position) performs connection signalr trigger OnConnectedAsync (client connection function, and stores the client identifier ConnectionId). 

Code Description:

// / <Summary> 
// Time / client connection call 
// / </ Summary> 
// / <Returns> </ Returns> 
public OnConnectedAsync the override the Task () 
{ 
  var ID = the this .Context.ConnectionId; // customer identification 
    Trace.WriteLine ( "client connection success" );
     return base.OnConnectedAsync (); 
} // all linked client will be here

signalr have to send a message that is triggering mechanism let me know I want a message, a message that is a function of signalr front of the package calls directly as a carrier to send data to the server, the message is triggered functions within the server.

Scenario 1:

Without this mechanism (mechanism must let the server know that a user has a message), users connect only do not know what to do, to absolutely no sense.

If you have a notification message, but also to read the database will also be superfluous namely (user triggers a message server only know the news, write code that is no content ajax reach the trigger mechanism within the messaging server is a function of the content of the news did not come to pass , internal trigger controller ajax method for storing messages to write to the database), then the front end of a signalr message notification mechanism may have the effect of jitter, this form yourself go read the message body and achieve real-time connection completely isolated, news format body are many support text, pictures, video, gif, and so we need to do processing (resource format supports multiple browsers such as Apple's video resources browser and google video resources) according to each end of the link resource format support , real-time connection compatibility issues here - every browser supports real-time connection has been workload is not small.

 The trigger mechanism is signar send a message that can carry the message body of the resource, where an official may not deliberately mentioned the details of it, after all browsers support, I can not support your message resource format it, certainly not that way.

Imagine 2: ConnectionId to bind the user nickname and avatar vice versa. ConnectionId here is inherited Hub class in such can access,

ConnectionId to bind Nickname and Avatar:

The first time as long as the link is available through the trigger OnConnectedAsync from the database and binding, and because of ConnectionId been persistent in the server, the binding relationship, but also persistent with static or cached on the server.

Nickname and avatar to bind ConnectionId:

By custom function, link, active trigger, the user name and picture spread in the custom function within the hub, obtain and binding, under similar diagram.

Third, the manner of the message

Buddy lists and message lists two cases

Already know: Can you get links to all clients (this link will be very large, how many million people links, but very few people do their own accounts),

1, so a user just landed situation:

The user must read the database all your friends, you will link all clients have full lighting of the user's friends.

2, the packet is the same as above

 

3, if the situation news

3,1 in the case of the one sent to our online Signalr transit service by the client, we first store database set up unread, the online push, otherwise canceled

3,2 the customer on the line but the user first read the list of users lit, and then have to say the news,

  Determining whether there are unread messages, and message with a message is read from the database to change the status read.

  And according to whether the user is online, the success of the anti-push message read id-- representation showing the effect has been read. (Knowing whether the message needs to be read, the operation is performed)

 

3, the message is recorded, direct access message record (knowing whether the message needs to be read, the message will be read to perform the operation state is acquired)

Guess you like

Origin www.cnblogs.com/fger/p/11607611.html