Log in and Log out
Update time: 2024/03/14 18:45:31
Login and logout
- During the connection process, the SDK will also execute related login logics. If login is successful, related login information can be returned in the
onconnect
callback of initialization configuration. If the login fails, related information can be returned in theondisconnect
callback. - To initialize the connection status, see "Initialize the SDK".
Logout
- After "Initialize the SDK", the SDK will log in automatically.
- After receiving
onconnect
callback, you can invokenim.disconnect();
to log out SDK. - After logging out SDK, you can invoke
nim.connect();
to log in SDK again.
Switching accounts
If you need to switch the IM account, you shall disconnect the Internet and then reconnect with a new account. Operation steps:
- Invoke "Log out IM" to log in IM;
- Invoke "Initialize the SDK" to initialize new IM.
If the developer needs to make seamless switching, they can retain IM connection instances of different accounts and the upper-level developers manage different accounts.
javascript var nim1 = NIM.getInstance({
account: 'nim1',
// ...
})
var nim2 = NIM.getInstance({
account: 'nim2',
// ...
})
// nim1 nim2 ... nimN will receive and send messages and have other IM features. Keep corresponding instance methods independent and browser performances operate stably
Update IM configuration
The SDK is designed as singleton pattern. If you need to update current IM configurations (some configurations cannot be updated, for example, user account), you can complete updates by the following two methods:
- You can invoke
NIM.getInstance
again. You only need to input configurations to be updated (The method is not recommended. The improper call results in logic conflicts of instances). - You can invoke
setOptions
for original instances. The parameter list and format shall be consistent with NIM.getInstance. Updating token is taken for an example.
javascript// Disconnect IM
nim.disconnect();
// Update a token
nim.setOptions({
token: 'newToken'
});
// Reconnect
nim.connect();
Multi-device login
CommsEase supports multi-device login. Users can log in to the same account from mobile clients and web client simultaneously.
Initialization parameters
- Not all initialization parameters are listed here. Please refer to Initialize SDK, and initialization parameters are given in other chapters.
- Initialization parameters for connection
- Initialization parameters for multi-device login
- Initialization parameters for messages
- Initialization parameters for team
- Initialization parameters for user profile
- Initialization parameters for friendship
- Initialization parameters for user relationship
- Initialization parameters for session
- Initialization parameters for system notifications
- Synchronization completed
- Complete initialization codes
Sample codes
javascriptvar nim = NIM.getInstance({
onloginportschange: onLoginPortsChange
});
function onLoginPortsChange(loginPorts) {
console.log('The status of the current login account has changed on the other client', loginPorts);
}
Parameter description
onloginportschange
: It is the callback of changing multi-device login status, and will receive Login client list. You can receive the callback when:- Other clients are online during login;
- Other clients are online or offline after login.
Login client
Login client represents related information of a certain login device, with fields as below:
type
: Device type for loginos
: OS of login devicemac
: mac address of login devicedeviceId
: Login device ID, uuidaccount
: Login accountconnectionId
: Connection account distributed by login deviceip
: IP of login servertime
: Login timeonline
: online or not.
Device types
Now, CommsEase supports several login clients as below:
'Android'
(Android)'iOS'
(Apple)'PC'
(Desktop. windows by default.)'Web'
(Explorer)'Mac'
(Desktop)
Validate previous login
Sample codes
javascriptnim.kick({
deviceIds: ['deviceId1'],
done: onKick
});
function onKick(error, obj) {
console.log('Remove other client’ + (!error?'succeeded':'failed'));
console.log(error);
console.log(obj);
}
Parameter description
- You can get device numbers at other login clients in
onloginportschange
callback. Please refer to Object of login client.