initSDK
Feature Introduction
Note
Initialization is the first step for the client to call the SDK. Within the lifespan of the application, it can only be called once. Repeated calls might lead to unforeseen problems.
Warning
(1) After successful initialization, set various listeners, with examples provided in the respective Quick Integration sections for each platform; (2) The client's underlying logs will be stored in the specified directory, available for debugging.
- iOS
- Android
- Flutter
- uni-app
- Browser/Electron/MiniProgram
- React-Native
- Unity
Function Prototype
// Method One
Future<dynamic> initSDK({
required int platform,
required String apiAddr,
required String wsAddr,
required String dataDir,
required OnConnectListener listener,
int logLevel = 6,
bool isLogStandardOutput = true,
String? logFilePath,
String? operationID,
});
// Method Two
Future<bool?> init(
InitConfig config,
OnConnectListener listener, {
String? operationID,
});
Input Parameters
Parameter Name | Parameter Type | Mandatory | Description |
---|---|---|---|
config | InitConfig | Yes | Initialization parameters |
Return Result
Name | Data Type | Description |
---|---|---|
~ | dynamic | Indicator of initialization success |
Code Example
OpenIM.iMManager.initSDK(
platform: IMPlatform.android,
apiAddr: '',
wsAddr: '',
dataDir: '',
listener: OnConnectListener(
onConnectFailed: (code, errorMsg) {},
onConnecting: () {},
onConnectSuccess: () {},
onKickedOffline: () {},
onUserTokenExpired: () {},
),
);
Function Prototype
- (BOOL)initSDKWithConfig:(OIMInitConfig *)config
onConnecting:(nullable OIMVoidCallback)onConnecting
onConnectFailure:(nullable OIMFailureCallback)onConnectFailure
onConnectSuccess:(nullable OIMVoidCallback)onConnectSuccess
onKickedOffline:(nullable OIMVoidCallback)onKickedOffline
onUserTokenExpired:(nullable OIMVoidCallback)onUserTokenExpired;
Input Parameters
Parameter Name | Parameter Type | Mandatory | Description |
---|---|---|---|
config | OIMInitConfig | Yes | Initialization parameters |
Return Result
Name | Data Type | Description |
---|---|---|
success | BOOL | Indicator of initialization success |
onConnecting | OIMVoidCallback | Connecting callback |
onConnectFailure | OIMFailureCallback | Connection failure callback |
onConnectSuccess | OIMFailureCallback | Connection success callback |
onKickedOffline | OIMVoidCallback | Kicked offline callback |
onUserTokenExpired | OIMVoidCallback | Token expiration callback |
Code Example
OIMInitConfig *config = [OIMInitConfig new];
config.apiAddr = @"";
config.wsAddr = @"";
config.objectStorage = @"";
BOOL success = [OIMManager.manager initSDKWithConfig:config
onConnecting:^{
} onConnectFailure:^(NSInteger code, NSString * _Nullable msg) {
// Connection failure callback function
// code error code
// error error message
} onConnectSuccess:^{
// SDK successfully connected to the IM server
} onKickedOffline:^{
// SDK is connecting to the IM server
} onUserTokenExpired:^{
// Online ticket has expired: At this point, you need to generate a new token and call the login() function again to log in.
}];
Parameter Details
public boolean initSDK(Application application, InitConfig initConfig, @NotNull OnConnListener listener)
Parameter Name | Parameter Type | Required | Description |
---|---|---|---|
application | Application | Yes | Application context |
initConfig | InitConfig | Yes | Initialization parameters |
listener | OnConnectListener | Yes | Listener |
Code Example
InitConfig initConfig = new InitConfig(
Constant.getImApiUrl(), //SDK API address
Constant.getImWsUrl(), //SDK WebSocket address
getStorageDir(), //SDK database storage directory
);
OpenIMClient.getInstance().initSDK(application,
initConfig,
new OnConnListener() {
@Override
public void onConnectFailed(long code, String error) {
//Connection to the server failed
}
@Override
public void onConnectSuccess() {
//Successfully connected to the server
}
@Override
public void onConnecting() {
//Connecting to the server...
}
@Override
public void onKickedOffline() {
//The current user was kicked offline
}
@Override
public void onUserTokenExpired() {
//Login token has expired
}
});
Note
The JS SDK doesn't require initialization, directly call login.
Function Prototype
IMSDK.asyncApi("initSDK", operationID: string, config: InitConfig): Promise<boolean>
Input Parameters
Parameter Name | Parameter Type | Required | Description |
---|---|---|---|
operationID | string | Yes | Operation ID, used for issue tracking, ensure uniqueness. Recommended to use current time and random number |
config | InitConfig | Yes | Initialization parameters |
Return Result
Using the
openim-uniapp-polyfill
package, the function is promised, so usethen
andcatch
to determine and handle success or failure callbacks.
Parameter Name | Parameter Type | Description |
---|---|---|
Promise.then() | Promise<boolean> | Whether initialization was successful |
Promise.catch() | Promise<CatchResponse> | Call failure callback |
Code Example
// App.vue
import IMSDK from 'openim-uniapp-polyfill';
const path = await getDbDir();
const config = {
apiAddr: 'http://xxx:10002', // IM API interface address. For example: http://xxx:10002
wsAddr: 'ws://xxx:10001', // IM websocket address. For example: ws://xxx:10001
platformID: uni.$u.os() === 'ios' ? 1 : 2, // Platform, refer to Platform class,
dataDir: path, // Data storage path
logLevel: 6, // Logging level
logFilePath: path, // Directory where logs are stored
isLogStandardOutput: true, // Whether to output to console
};
const flag = await IMSDK.asyncApi('initSDK', IMSDK.uuid(), config);
// utils.js
export const getDbDir = () =>
new Promise((resolve, reject) => {
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fs) => {
fs.root.getDirectory(
'user',
{
create: true,
},
(entry) => {
resolve(entry.fullPath);
},
(error) => {
reject(error);
}
);
});
});
Function Prototype
OpenIMSDKRN.initSDK(config: InitConfig, optionalID:string): Promise<boolean>
Input Parameters
Parameter Name | Parameter Type | Mandatory | Description |
---|---|---|---|
config | InitConfig | Yes | Initialization parameters |
operationID | string | Yes | Operation ID, used for problem location, keep unique, suggest using current time and random number |
Return Result
Parameter Name | Parameter Type | Description |
---|---|---|
Promise.then() | Promise<boolean> | Whether initialization was successful |
Promise.catch() | Promise<CatchResponse> | Callback on failed call |
Code Example
import OpenIMSDKRN from 'open-im-sdk-rn';
import RNFS from 'react-native-fs';
RNFS.mkdir(RNFS.DocumentDirectoryPath + '/tmp');
OpenIMSDKRN.initSDK({
platformID: 2, // 1: ios, 2: android
apiAddr: 'http://your-server-ip:10002',
wsAddr: 'ws://your-server-ip:10001',
dataDir: RNFS.DocumentDirectoryPath + '/tmp',
logLevel: 5,
isLogStandardOutput: true,
}, 'operationID');
Function Prototype
public static bool InitSDK(IMConfig config, IConnCallBack cb)
Input Parameter
Parameter Name | Parameter Type | Mandatory | Description |
---|---|---|---|
config | IMConfig | Yes | Initialization parameters |
cb | IConnCallBack | Yes | Network Status Interface |
Code Example
var config = new IMConfig()
{
PlatformID = (int)PlatformID,
WsAddr = wsAddr,
ApiAddr = apiAddr,
DataDir = dataDir,
LogLevel = 5,
IsLogStandardOutput = true,
LogFilePath = logDir,
IsExternalExtensions = true,
};
var connListener = new IConnListener();
var res = IMSDK.InitSDK(config, connListener);