If we develop the instant communication system (IM System) to support the same account multi-device simultaneous login scene (or "multi-login"), that is, like QQ, in the PC-side login, but also can use the same account login mobile (iOS or Android), then, how to do it?
In the Esframework/esplatform system, the userid is used as a unique flag to mark each user, that is, only one client can be online for a specified userid. Therefore, although esframework supports multiple types of client devices, Esframework does not support different types of client devices logging on to the same userid at the same time.
The rationale for the solution is simple: Since esframework requires UserID to be unique as a user token, we introduce a concept called "LoginID" that uses different LoginID on different types of devices for the same user. But these loginid all point to the same real userid.
I. LoginID and the real UserID1. No need to support the simple scenario of simultaneous login with the account multiple devices
In the scenario where the same account multiple devices are not supported at the same time ("single Device Login" scenario), the login account is the real userid, This means that the user account that is involved in the parameters of each API (each method and event) in the Esframework framework is a true userid. For example, an account abc001, which is present in the database user table, using abc001 to log on to the Esframework server, esframework throughout the operation, but also use abc001 to mark the corresponding client instance. In this scenario, there is no case where multiple running client instances correspond to account abc001. If a client has already logged in with abc001 and then uses that account to log in elsewhere, the default mechanism is to squeeze the client that was previously logged in.
2. Need to support multi-device simultaneous login with the account complex scene
If now we want to support the same account multi-device simultaneous login scene ("Multi-Device Login" scenario), then, esframework throughout the operation, you can not use abc001 to mark the corresponding client instance, Because there are multiple client instances that correspond to the same abc001 account. Therefore, we use loginID to distinguish between different client instances in this case.
A common method is to prefix the real userid with a two-character suffix to form the loginID. For example, for abc001 this account, when using iOS device login, we choose to use the prefix "#", so the iOS device uses loginID is 1#abc001, the same way, the Android device uses 2#abc001.
The meaning of the two-character prefix is this:
(1) The second character, "#", is a flag (token) that indicates that the ID is a loginid.
(2) The first character that represents the type of device. For example, "0" means. NET device (PC), "1" means iOS device, "2" means Android device, and so on.
When loginID is used, esframework no longer uses the real UserID to mark the client instance in the entire operation, but instead uses the loginID-that is, The user accounts that are involved in the parameters of each API (each method and event) in the Esframework framework are loginID .
Two. Multidevicehelper class
The Multidevicehelper class is available in the Esframework.boost class library to provide support for simultaneous multi-device logons. In particular, it provides APIs related to the construction and parsing of loginID.
In the static constructor of Multidevicehelper, a prefix is specified for each device, as follows:
static MultiDeviceHelper ()
{
#region If there is no device of some type in the current application, comment out the corresponding statement below.
MultiDeviceHelper.LoginIDPrefixMapping.Add (ClientType.IOS, "1 #");
MultiDeviceHelper.LoginIDPrefixMapping.Add (ClientType.Android, "2 #");
MultiDeviceHelper.LoginIDPrefixMapping.Add (ClientType.WebSocket, "3 #");
MultiDeviceHelper.LoginIDPrefixMapping.Add (ClientType.WindowsPhone, "4 #");
#endregion
}
Multidevicehelper then provides several static methods to complete the conversion between the true UserID, the device type, and the loginID:
Three. Login and login Verification
The client calls Irapidpassiveengine's Initialize method when it logs on:
Logonresponse Initialize (string UserID, string logonpassword, string serverip, int ServerPort, Icustomizehandler Customizehandler);
The first parameter of the method needs to be passed in loginID, such as 1#abc001.
The server will callback the Ibasichandler interface's Verifyuser method for account password Authentication:
BOOL Verifyuser (string systemtoken, string UserID, string password, out string Failurecause);
It is important to note that the UserID parameter passed in by the Verifyuser method is actually loginID, or 1#abc001. We need to get the real userid by calling Multidevicehelper's Parseloginid method, which returns ABC001, and the out parameter indicates that the device type is iOS.
Four. Handling messages and other
In the Esframework architecture, the server handles the received message through the Handleinformation method of the callback Icustomizehandler interface:
void Handleinformation (string sourceuserid, int informationtype, byte[] info);
As above, the Sourceuserid parameter here is actually loginID, so you also need to call Multidevicehelper's Parseloginid method to convert it to a real userid.
Similarly, in a multi-device login scenario, the UserID parameters for each API (each method and event) in the Esframework framework are actually loginid, and they need to be handled in a similar way, not listed here.
Five. Multi-device Chat message synchronization
After solving the problem of multi-device simultaneous login, there is a common need: similar to QQ pc and mobile phone side while online, others send me a message, mobile phone and PC side can receive. How is this function implemented?
In a single-device login scenario, we typically send a chat message on the following send method that the client invokes the Icustomizeoutter interface:
void Send (string targetuserid, int informationtype, byte[] info);
The first parameter of the method is the recipient's userid, which means that the chat message is sent directly to the other party (either through the server or directly via a peer-to channel).
However, in a multi-device login scenario, you can no longer send directly, but you must go through the server by calling the following send method:
void Send (int informationtype, byte[] info);
The Send method sends the message directly to the server and contains the UserID for the message recipient in the info parameter. When the server processes the message, it needs to parse the recipient UserID from info, and then call Multidevicehelper's Getloginidlist method to get loginID for each device type, and then The server is sending the message to every loginid. In this way, both the mobile phone and the PC end will receive this chat message.
For information on how to use the Esframework communication framework, refer to the Esframework development manual.
How to achieve in the instant communication system: Support the PC side and mobile side simultaneously online (that is, support the same account multiple devices simultaneously login)?