重构应用程序架构,添加 OpenIM SDK 初始化和引导逻辑
This commit is contained in:
38
features/im/openim-bootstrap.ts
Normal file
38
features/im/openim-bootstrap.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import OpenIMSDK, { MessageItem } from "@openim/rn-client-sdk";
|
||||
import RNFS from "react-native-fs";
|
||||
|
||||
const OPENIM_DIR = `${RNFS.DocumentDirectoryPath}/openim`;
|
||||
|
||||
let initialized = false;
|
||||
let ensureDirPromise: Promise<void> | null = null;
|
||||
|
||||
function ensureOpenIMDir() {
|
||||
if (!ensureDirPromise) {
|
||||
ensureDirPromise = RNFS.mkdir(OPENIM_DIR).then(() => undefined);
|
||||
}
|
||||
|
||||
return ensureDirPromise;
|
||||
}
|
||||
|
||||
export async function bootstrapOpenIM() {
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
await ensureOpenIMDir();
|
||||
|
||||
OpenIMSDK.initSDK({
|
||||
apiAddr: "https://openim-api.riwsan.com/api",
|
||||
wsAddr: "wss://openim-api.riwsan.com/msg_gateway",
|
||||
dataDir: OPENIM_DIR,
|
||||
logFilePath: OPENIM_DIR,
|
||||
logLevel: 5,
|
||||
isLogStandardOutput: true,
|
||||
});
|
||||
|
||||
OpenIMSDK.on("onRecvNewMessages", (messages: MessageItem[]) => {
|
||||
console.log("onRecvNewMessages", messages);
|
||||
});
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
Reference in New Issue
Block a user