How to make C / S applications support multi-terminal (PC, Android, iOS) simultaneously logged?

In the C / S architecture, usually using a UserID as the only flag to mark each user, that is, for a given UserID, only one client online.

If we develop a system to support the same account multiple devices simultaneously log scenes that need like micro-letters, like, at the same time the PC login, you can also log on mobile end (iOS or Android) using the same account, then how can we do the ?

Principle solution is relatively simple: Since the C / S system requirements UserID as user mark must be unique, then we introduced a concept called “LoginID”, for the same user, it is used on different types of devices different LoginID, but they all point to the same LoginID real UserID.

A. LoginID and UserID

1. The need to support multiple devices simultaneously log in with an account of a simple scene

In the scene before the same account does not support multiple devices simultaneously log in (referred to as “Single Device Login” scene), log in with the account is true UserID, that is the underlying framework of each API (various methods, and events) of the parameters involved in user accounts are real UserID. For example, abc001 an account, the account number is present in the user table in the database; logon to server using abc001, throughout the course of the operation, using abc001 correct service to the client instance corresponding to the mark. In this scenario, without the presence of multiple instances of the client running account corresponds to abc001. If there are clients already using abc001 login, and then use the account login in other places, the default mechanism is that the client logs will be squeezed out before the line.

2. The need to support multiple devices simultaneously log in with an account of complex scenes

If now we want to support scenarios with multiple devices simultaneously log of account (referred to as “multiport Login” scene), then, the server throughout the course of the operation, will not be able to use abc001 client instances corresponding to the mark, because there are multiple examples of the client corresponds to a case where the same abc001 account. Thus, we use to distinguish different LoginID client instance in this case.

Commonly used method is before the real UserID prefixed with two characters to form LoginID. For example, for abc001 this account, when using an iOS device to log, we chose to use the prefix “# 1”, so that iOS devices use LoginID # 1 abc001; Similarly, Android devices will use 2 # abc001.

The two characters meaning prefix is ​​this:

(1) The second character “#”, is a sign (token), it indicates that the ID is a LoginID.

(2) The first character indicating the type of device. For example, “0” indicates .NET device (PC), “1” represents iOS, “2” denotes Android, and the like.

When using LoginID, the server throughout the course of the operation are no longer using real UserID to mark the client instance, but rather use LoginID – that is, in the framework of the various API (various methods, and events) parameters related to the user account is a LoginID.


II. MultiDeviceHelper Class

I wrote a MultiDeviceHelper class, it is used to support multiple devices simultaneously log. In particular, it provides construction and related LoginID parsing API.

In the static constructor MultiDeviceHelper, each device a predetermined prefix, as follows:

    static MultiDeviceHelper()
        #region 如果在当前的应用中,不存在某种类型的设备,则注释掉下面对应的语句即可。            
        MultiDeviceHelper.LoginIDPrefixMapping.Add(ClientType.IOS, "1#");
        MultiDeviceHelper.LoginIDPrefixMapping.Add(ClientType.Android, "2#");
        MultiDeviceHelper.LoginIDPrefixMapping.Add(ClientType.DotNet, "3#"); 

Then, MultiDeviceHelper provides several static methods to complete the conversion between the real UserID, a device type and LoginID:



III. Login and login authentication

Client when logging calls IRapidPassiveEngine the Initialize method:

       LogonResponse Initialize(string userID, string logonPassword, string serverIP, int serverPort, ICustomizeHandler customizeHandler); 

The first parameter of this method would need to pass LoginID, such as 1 # abc001.

In VerifyUser method server calls back IBasicHandler interface to verify the account password:

        bool VerifyUser(string systemToken, string userID, string password, out string failureCause);        

At this point to note is that, VerifyUser method passed in userID parameter is actually LoginID, namely 1 # abc001. We need to get by calling ParseLoginID method MultiDeviceHelper real UserID, the method returns abc001, and out parameter indicates the type of device to iOS.


IV. Processing messages and other

Server to process the message is received by the callback method ICustomizeHandler HandleInformation interface:

       void HandleInformation(string sourceUserID, int informationType, byte[] info); 

Same as above, sourceUserID parameters here actually LoginID, therefore, also need to call ParseLoginID MultiDeviceHelper method to convert it into real UserID.

Similarly, in the multi-device log on the scene, in the framework of the various API (various methods, and events) of userID parameters are actually LoginID, when dealing with similar processing needs to be done, not list them here.


V. Multi chat message synchronization device

In solving the problem of multi-device log on simultaneously after, there is a common requirement: when similar QQ online at the same end PC and mobile phone, someone sent me a message, mobile terminal and PC terminal can receive. This function is how to achieve it?

In a single device login scenario, we usually call the following customer interface Send method ICustomizeOutter end to send a chat message:

       void Send(string targetUserID, int informationType, byte[] info); 

The first method is a parameter of the recipient UserID, a direct send chat messages to each other (perhaps through the relay server, or transmitted directly via P2P channel).

However, in the multi-device log on the scene and it can not be sent directly, but must go through the server transfer, by calling Send the following methods:

       void Send(int informationType, byte[] info); 

The Send method sends a message directly to the server, comprising a message to a recipient in UserID info parameter. When the server processes the message, will need to parse info from the UserID recipient, then call GetLoginIDList MultiDeviceHelper method to acquire the respective devices corresponding to the type LoginID, then the server sends the message to each of a LoginID. Thus, mobile terminal and PC terminal can receive this chat on the news.


(Note: The latest version of ESFramework.MSide.dll has built-in support for multi-terminal log on simultaneously, that is, the principles set forth herein have been implemented in the framework of ESFramework In addition, OrayTalk also increased many-fold function login. downloadable test.)


Leave a Reply