Electron IMLib 初始化

初始化构造客户端实例:初始化 SDK(构造客户端实例)。幂等:同 appkey 重复初始化返回同一实例。

初始化选项​

字段类型必填说明
appkeystring是控制台应用 AppKey,必填
serverUrlstring否IM 服务地址(含协议与端口)
timeoutMsuint32否请求超时(毫秒)
transportany否自定义传输实现(高级用法,一般无需设置)

平台扩展选项​

{appkey: string, dbPath?: string, logLevel?: LogL, environment?: string, syncEmptyTopConversation?: boolean, disableLogReport?: boolean, httpInMainProcess?: boolean, eventLoopBlockingCheck?: boolean}

工厂:initService(options) → { client, destroy, capabilities, ... }(主进程唯一实例)。

Electron 主进程服务(本地库 / 设备信息):initService(options): ServiceHandle { destroy(): void; getDeviceId(): string; getServerTime(): number }。

初始化示例​

JavaScript (ESM)
// 主进程 main.js:初始化主进程服务(IMLib + 本地 SQLite)
import { app } from 'electron';
import { initService } from '@ni-paas-im/electron/main';

const service = initService({
  appkey: 'YOUR_APP_KEY',
  serverUrl: 'https://im.example.com',
  dbPath: app.getPath('userData'),   // 目录:库文件在 <dbPath>/im-electron/<appkey>/<userId>.db
});
const client = service.client;        // 主进程唯一的 IMLib 客户端

幂等与生命周期​

  • init 幂等:同 appkey 重复初始化返回同一实例(IDL init.idempotent = true),不会重复建立连接。
  • 初始化不建立长连接;必须再调用 connect(token)。
  • 应用退出 / 页面销毁时调用 disconnect() 停止重连并清理心跳;需要释放句柄时使用平台对应释放接口(如 Native im_client_free)。
  • 不要绕过 SDK 直接连接 WebSocket 或直接操作本地数据库,否则会破坏离线补发、去重与多端一致性。

初始化后做什么​

事件与错误

事件名来自 capabilityModel.events(16 个);失败码见 状态码。绑定状态:in-progress。