完整的 NIM SDK 由 NIMInterface, ChatroomServiceInterface 组成。
SDK 的入口,负责建立长连接,登录,断开长连接,销毁实例、多端互踢等功能。类的静态方法见 NIMInterfaceStatic
| 方法 | 功能描述 |
|---|---|
| NIMInterfaceStatic.getInstance | 单例模式获取实例 |
| NIMInterfaceStatic.setAdapters | 设置适配器 |
| NIMInterfaceStatic.registerService | 注册模块. ESM 模式使用 |
下面是NIMInterfaceStatic.getInstance的初始化参数。这里只例举部分回调函数,完整初始化参数见 NIMInitializeOptions,以及NIMOtherOptions
注意,初始化有两个参数,调用方式为: NIM.getInstance(param1, param2)。param1 为 NIMInitializeOptions, param2 为 NIMOtherOptions
// 0.11.0 版开始支持单例模式获取实例, 在此之前通过 new 创建实例
const nim = NIM.getInstance(
/**
* param1: NIMInitializeOptions
*/
{
appkey: 'YOUR_APPKEY',
token: 'YOUR_TOKEN',
account: 'YOUR_ACCOUNT'
},
/**
* param2: NIMOtherOptions
*/
{
/**
* 会话初始化设置
*/
sessionConfig: {
/**
* 用户可以根据应用场景,设置消息是否要计入未读数。
*/
unreadCountFilterFn: function (msg) {
return true
}
},
/**
* 同步初始化设置
*/
syncOptions: {
/**
* 是否同步置顶会话消息
*/
stickTopSessions: true,
/**
* 是否同步用户信息
*/
myInfo: false
}
}
)
提供最为基础的初始化参数。该参数的完整属性列表请参考NIMInitializeOptions
| 属性 | 功能描述 |
|---|---|
| NIMInitializeOptions.appkey | 应用 appkey |
| NIMInitializeOptions.account | IM 账户 |
| NIMInitializeOptions.token | IM 账户 token |
| NIMInitializeOptions.authType | token 的认证模式 |
| NIMInitializeOptions.debugLevel | 日志分级 |
提供一些扩展初始化参数。该参数的完整属性列表请参考NIMOtherOptions。
| 属性 | 功能描述 |
|---|---|
| NIMOtherOptions.cloudStorageConfig | cloud storage 模块配置 |
| NIMOtherOptions.sessionConfig | session 模块配置 |
| NIMOtherOptions.syncOptions | 同步模块的选项配置 |
调用nim.on(eventName),监听 nim 事件,注册事件相关的回调函数。下面是一些回调事件的定义,完整 API 请参见IMEventInterface
| 事件名称 | 功能描述 |
|---|---|
| IMEventInterface.logined | 初始化成功登陆 |
| IMEventInterface.multiPortLogin | 多端登陆通知 |
| IMEventInterface.kicked | 被踢下线 |
| IMEventInterface.willReconnect | 开始自动重连 |
| IMEventInterface.syncdone | 同步完成事件 |
| IMEventInterface.disconnect | 连接断开 |
| 事件名称 | 功能描述 |
|---|---|
| IMEventInterface.msg | 收到消息 |
| IMEventInterface.broadcastMsgs | 收到广播消息(在线或者离线同步收到均会触发) |
| IMEventInterface.deleteSelfMsgs | 收到了单向删除某消息的通知 |
| IMEventInterface.clearServerHistoryMsgs | 在初始化同步/多端同步时,收到了清除历史消息的通知 |
| IMEventInterface.syncRoamingMsgs | 同步时收到了漫游消息 |
| IMEventInterface.syncOfflineMsgs | 同步时收到了离线消息 |
| IMEventInterface.sessions | 同步时收到了会话 |
| IMEventInterface.sysMsg | 在线时收到了系统通知 |
| IMEventInterface.syncSysMsgs | 同步漫游/离线时收到的系统消息 |
| IMEventInterface.teamMsgReceipts | 收到了群已读 |
| IMEventInterface.updateSession | 会话更新 |
| 事件名称 | 功能描述 |
|---|---|
| IMEventInterface.updateUserInfo | 其他用户资料更新回调 |
| IMEventInterface.syncFriend | 多端同步时收到了好友相关的资料 |
| IMEventInterface.friends | 同步时收到了好友相关资料 |
| IMEventInterface.users | 同步时收到了其他用户相关资料 |
| IMEventInterface.relations | 同步收到的关系,包括黑名单和静音列表 |
| IMEventInterface.updateMyNameCard | 收到了更新个人信息通知 |
| IMEventInterface.updateBlackList | 收到了黑名单更新通知 |
| IMEventInterface.updateMuteList | 收到了静音列表更新通知 |
| IMEventInterface.syncMyNameCard | 同步时收到了个人信息 |
| 事件名称 | 功能描述 |
|---|---|
| IMEventInterface.teams | 收到了初始化同步的群 |
| IMEventInterface.createTeam | 收到了多端同步创建群的情况 |
| IMEventInterface.updateTeamMember | 收到了多端同步来更新群成员的情况 |
| IMEventInterface.updateTeam | 收到了群的更新信息 |
| IMEventInterface.addTeamMembers | 收到了群成员的更新 |
| IMEventInterface.updateTeamManagers | 收到了群管理员的更新 |
| IMEventInterface.transferTeam | 收到了转让群 |
| IMEventInterface.removeTeamMembers | 收到成员离群 |
| IMEventInterface.dismissTeam | 触发了解散群 |
| IMEventInterface.updateTeamMembersMute | 收到了更新群成员静音 |
| 事件名称 | 功能描述 |
|---|---|
| IMEventInterface.superTeams | 收到了初始化同步的群 |
| IMEventInterface.updateSuperTeam | 超级群更新 |
| IMEventInterface.addSuperTeamMembers | 超级群添加 |
| IMEventInterface.removeSuperTeamMembers | 移除超级群成员 |
| IMEventInterface.updateSuperTeamManagers | 超级群管理员更新 |
| IMEventInterface.transferSuperTeam | 触发转让超级群事件 |
| IMEventInterface.updateSuperTeamMembersMute | 超级群成员静音事件 |
| IMEventInterface.updateSuperTeamMember | 超级群成员更新的多端同步 |
| IMEventInterface.dismissSuperTeam | 超级群解散 |
| 事件名称 | 功能描述 |
|---|---|
| IMEventInterface.updateSystemMessages | 收到了系统消息 |
| IMEventInterface.pushEvents | 收到了订阅事件 |
| IMEventInterface.proxyMsg | 收到了代理消息(透传的) |
nim 连接管理相关函数。调用方式为nim.connect,nim.destroy等
以下 API 挂载在 nim 实例对象上。
| 方法/属性 | 功能描述 |
|---|---|
| NIMInterface.status | 实例状态 |
| NIMInterface.connect | 建立长连接,并且登录 |
| NIMInterface.disconnect | 断开连接。 |
| NIMInterface.destroy | 销毁实例 |
| NIMInterface.kick | 踢人 |
挂载了消息相关的 API,如发送消息,发送文件消息,撤回,已读等。调用示例: nim.msg.sendTextMsg。完整的 API 请参见 MsgServiceInterface
| 方法 | 功能描述 |
|---|---|
| MsgServiceInterface.sendTextMsg | 发送文本消息 |
| MsgServiceInterface.sendTipMsg | 发送提醒消息 |
| MsgServiceInterface.sendGeoLocationMsg | 发送地理位置信息 |
| MsgServiceInterface.sendCustomMsg | 发送自定义消息 |
| MsgServiceInterface.sendFileMsg | 发送普通文件消息 |
| MsgServiceInterface.sendImageMsg | 发送图片消息 |
| MsgServiceInterface.sendVideoMsg | 发送视频消息 |
| MsgServiceInterface.sendAudioMsg | 发送音频消息 |
| MsgServiceInterface.forwardMsg | 转发消息 |
| MsgServiceInterface.recallMsg | 撤回消息 |
| MsgServiceInterface.deleteSelfMsgs | 单向删除消息 |
| MsgServiceInterface.sendMsgReceipt | 发送已读标记 |
| MsgServiceInterface.sendTeamMsgReceipt | 发送群已读标记 |
| MsgServiceInterface.getTeamMsgReads | 获取群消息已读的数量 |
| MsgServiceInterface.getTeamMsgReadAccounts | 获取群消息已读的账号 |
挂载了消息管理相关的 API,如删除漫游消息等。调用示例: nim.msgLog.deleteRoamingMsgs。完整的 API 请参见 MsgLogServiceInterface
| 方法 | 功能描述 |
|---|---|
| MsgLogServiceInterface.deleteRoamingMsgs | 批量删除服务器上某些会话的漫游消息 |
挂载了订阅发布事件相关的能力,如订阅,发布事件等。调用示例: nim.event.publishEvent。完整的 API 请参见 EventServiceInterface
| 方法 | 功能描述 |
|---|---|
| EventServiceInterface.publishEvent | 发布某事件 |
| EventServiceInterface.subscribeEvent | 订阅某事件 |
| EventServiceInterface.unSubscribeEvents | 取消订阅 |
| EventServiceInterface.querySubscribeEvents | 查询所订阅的事件 |
挂载了好友相关的 API,如获取,添加,申请,删除好友等。调用示例: nim.friend.addFriend。完整的 API 请参见 FriendServiceInterface
| 方法 | 功能描述 |
|---|---|
| FriendServiceInterface.getFriends | 获取好友列表 |
| FriendServiceInterface.addFriend | 直接加为好友 |
| FriendServiceInterface.applyFriend | 申请加为好友 |
| FriendServiceInterface.deleteFriend | 删除好友 |
| FriendServiceInterface.updateFriend | 更新好友 |
挂载了透传协议。调用示例: nim.passThrough.request。完整的 API 请参见 PassThroughServiceInterface
| 方法 | 功能描述 |
|---|---|
| PassThroughServiceInterface.request | 透传协议 |
挂载了杂项能力 API,如 getServerTime 消息。调用示例: nim.misc.getServerTime。完整的 API 请参见 MiscServiceInterface
| 方法 | 功能描述 |
|---|---|
| MiscServiceInterface.getServerTime | 获取服务器时间 |
挂载了消息扩展相关的 API,如 thread 消息。调用示例: nim.msgExtend.getThreadMsgs。完整的 API 请参见 MsgExtendServiceInterface
| 方法 | 功能描述 |
|---|---|
| MsgExtendServiceInterface.getThreadMsgs | 获取 thread 消息列表 |
| MsgExtendServiceInterface.getMsgsByIdServer | 获取 thread 历史消息 |
挂载了会话相关的 API,如查看会话,重置会话等。调用示例: nim.session.getSession。完整的 API 请参见 SessionServiceInterface
| 方法 | 功能描述 |
|---|---|
| SessionServiceInterface.getSession | 获取会话 |
| SessionServiceInterface.getSessions | 获取会话列表 |
| SessionServiceInterface.getAllSessions | 获取全部的会话列表 |
| SessionServiceInterface.resetSessionUnreadCount | 重置某个会话的未读数 |
| SessionServiceInterface.resetAllSessionsUnreadCount | 重置所有会话的未读数 |
| SessionServiceInterface.deleteSession | 删除指定的会话 |
| SessionServiceInterface.deleteAllSessionsFromLocal | 删除所有本地的会话 |
以下 API 挂载在 nim.cloudSession 对象上。
挂载了系统消息相关的 API,如发送自定义系统通知。调用示例: nim.systemMessage.sendCustomSysMsg。完整的 API 请参见 SystemMessageServiceInterface
| 方法 | 功能描述 |
|---|---|
| SystemMessageServiceInterface.sendCustomSysMsg | 发送自定义系统通知 |
挂载了信令相关的 API。调用示例: nim.signaling.callEx。完整的 API 请参见 SignalingServiceInterface
信令部分的事件定义请参见 NIMESignalingEventInterface, 注意信令部分的事件并不是挂在全局事件中,而是挂在 signaling 模块下,调用形式如
nim.signaling.on('signalingClose', () => {})
| 事件名 | 功能描述 |
|---|---|
| NIMESignalingEventInterface.signalingClose | 收到频道关闭事件 |
| NIMESignalingEventInterface.signalingJoin | 收到成员加入频道事件 |
| NIMESignalingEventInterface.signalingInvite | 收到邀请事件 |
| NIMESignalingEventInterface.signalingCancelInvite | 收到取消邀请事件 |
| NIMESignalingEventInterface.signalingReject | 收到拒绝邀请事件 |
| NIMESignalingEventInterface.signalingAccept | 收到接受邀请事件 |
| NIMESignalingEventInterface.signalingLeave | 收到退出频道事件 |
| NIMESignalingEventInterface.signalingCustomCommand | 收到自定义控制指令事件 |
| NIMESignalingEventInterface.singalingSyncChannels | 初始化时同步获取到已加入的频道列表 |
挂载了群相关的 API,如获取,创建,离开群,群成员管理。调用示例: nim.team.getTeamInfo。完整的 API 请参见 TeamServiceInterface
挂载了超级群相关的 API,如获取,离开超级群,超级群成员管理。调用示例: nim.superTeam.getSuperTeamInfo。完整的 API 请参见 SuperTeamServiceInterface
挂载了用户及关系相关的 API,如黑名单,静音列表,我的名片。调用示例: nim.user.updateMyNameCard。完整的 API 请参见 UserServiceInterface
| 方法 | 功能描述 |
|---|---|
| UserServiceInterface.setBlack | 加入黑名单/从黑名单移除 |
| UserServiceInterface.setMute | 加入静音列表/从静音列表移除 |
| UserServiceInterface.getUsersNameCardFromServer | 获取用户名片数组 |
| UserServiceInterface.updateMyNameCard | 更新我的名片 |
| UserServiceInterface.getRelations | 获取关系(黑名单和静音列表) |
| UserServiceInterface.getBlackList | 获取黑名单列表 |
| UserServiceInterface.getMuteList | 获取静音列表 |
挂载了文件上传逻辑。调用示例: nim.cloudStorage.uploadFile。完整的 API 请参见 CloudStorageServiceInterface
| 方法 | 功能描述 |
|---|---|
| CloudStorageServiceInterface.uploadFile | 上传文件 |
| CloudStorageServiceInterface.getOriginUrl | 短链转长链 |
| CloudStorageServiceInterface.getFileToken | 获取文件鉴权的 token |