跳到主要内容

🚀使用 Demo

我们强烈建议您首先运行我们为您准备的框架相关的示例 DEMO 。这不仅可以让您直观体验 OpenIMSDK 的功能,还将帮助您在实际集成过程中迅速定位和解决问题。

提示

项目刚刚启动还在更新中,急需功能的需要自行添加。

2. 导入包

  import OpenIMSDKRN from "open-im-sdk-rn";

3. 初始化

    const Init = async () => {
await RNFS.mkdir(RNFS.DocumentDirectoryPath + '/tmp');
const config = {
platformID: platform,
apiAddr: API_URL,
wsAddr: WS_URL,

dataDir: RNFS.DocumentDirectoryPath + '/tmp',
logLevel: 6,
isLogStandardOutput: true,
};
try {
const opid = "123456";
const result = await OpenIMSDKRN.initSDK(config, opid);

} catch (error) {
console.error('Error initializing SDK:', error); // Log the error
}
};
const checkLogin = async () => {
const status = (await GetLoginStatus()).status;
if (status == 3){
setIsLoggedIn(true)
initStore()
}

}

4. 设置监听器

    const setIMListener = () => {

// account
OpenIMEmitter.addListener('onSelfInfoUpdated', (v) => { selfUpdateHandler });
OpenIMEmitter.addListener('onConnecting', connectingHandler);
OpenIMEmitter.addListener('onConnectFailed', connectFailedHandler);
OpenIMEmitter.addListener('onConnectSuccess', connectSuccessHandler);
OpenIMEmitter.addListener('onKickedOffline', (v) => { kickHandler });
OpenIMEmitter.addListener('onUserTokenExpired', (v) => { expiredHandler });
// sync
OpenIMEmitter.addListener('onSyncServerStart', syncStartHandler );
OpenIMEmitter.addListener('onSyncServerFinish', syncFinishHandler );
OpenIMEmitter.addListener('onSyncServerFailed', syncFailedHandler );
// message
OpenIMEmitter.addListener('onRecvNewMessage', newMessageHandler );
OpenIMEmitter.addListener('onRecvNewMessages', newMessageHandler );
OpenIMEmitter.addListener('onNewRecvMessageRevoked',revokedMessageHandler );
// // conversation
OpenIMEmitter.addListener('onConversationChanged', (v) => { conversationChnageHandler });
OpenIMEmitter.addListener('onNewConversation', (v) => { newConversationHandler });
OpenIMEmitter.addListener('onTotalUnreadMessageCountChanged', (v) => { totalUnreadChangeHandler });
// // friend
OpenIMEmitter.addListener('onFriendInfoChanged', friendInfoChangeHandler );
OpenIMEmitter.addListener('onFriendAdded', friendAddedHandler );
OpenIMEmitter.addListener('onFriendDeleted', (v) => { friendDeletedHandler });
// // blacklist
OpenIMEmitter.addListener('onBlackAdded', (v) => { blackAddedHandler });
OpenIMEmitter.addListener('onBlackDeleted', (v) => { blackDeletedHandler });
// // group
OpenIMEmitter.addListener('onJoinedGroupAdded', (v) => { joinedGroupAddedHandler });
OpenIMEmitter.addListener('onJoinedGroupDeleted', (v) => { joinedGroupDeletedHandler });
OpenIMEmitter.addListener('onGroupInfoChanged', (v) => { groupInfoChangedHandler });
OpenIMEmitter.addListener('onGroupMemberAdded', (v) => { groupMemberAddedHandler });
OpenIMEmitter.addListener('onGroupMemberDeleted', (v) => { groupMemberDeletedHandler });
OpenIMEmitter.addListener('onGroupMemberInfoChanged', (v) => { groupMemberInfoChangedHandler });
// // application
OpenIMEmitter.addListener('onFriendApplicationAdded', friendApplicationProcessedHandler );
OpenIMEmitter.addListener('onFriendApplicationAccepted', friendApplicationProcessedHandler );
OpenIMEmitter.addListener('onFriendApplicationRejected', friendApplicationProcessedHandler );
OpenIMEmitter.addListener('onGroupApplicationAdded', (v) => { groupApplicationProcessedHandler });
OpenIMEmitter.addListener('onGroupApplicationAccepted', (v) => { groupApplicationProcessedHandler });
OpenIMEmitter.addListener('onGroupApplicationRejected', (v) => { groupApplicationProcessedHandler });
};