SDK · uni-app

@imcore/sdk/uni-app provides the typed imcore API for uni-app. It adapts the injected uni API’s asynchronous storage, SocketTask, lifecycle/network events and uploadFile; it has no dependency on a particular uni-app renderer.

Install

npm install ./imcore-sdk-js-<version>.tgz

Build the target with uni-app’s normal tooling. Configure the IM hosts as WebSocket and upload request domains for every target platform. Production deployments must use wss:// and https://.

Create one application client

import { createUniAppClient } from '@imcore/sdk/uni-app';

const im = createUniAppClient({
  uni,
  url: 'wss://im.example.com/acc',
  auth: {
    getToken: () => uni.getStorageSync('access_token'),
    login: { userID: '1001', userName: 'alice' },
  },
  storageNamespace: '@my-app/im/1001/',
});

export default {
  async onLaunch() {
    await im.start();
  },
};

start() observes uni.onAppShow, uni.onAppHide and uni.onNetworkStatusChange. It releases the socket while the app is hidden by default and reconnects after the app becomes visible or network returns. Call im.stop() on logout, and use a distinct storageNamespace for every account.

Upload media

Pass a temporary path from uni.chooseImage, uni.chooseVideo, or the target platform’s file picker. The returned segment can be sent directly.

const picked = await uni.chooseImage({ count: 1 });
const uploaded = await im.media.upload({
  file: picked.tempFilePaths[0],
  type: 'image',
  scene: 'dm',
  targetUserID: '1002',
});
await im.dm.send({ targetUserID: '1002', segments: [uploaded.segment] });

The upload origin defaults to the WebSocket host. Pass httpBaseUrl when the HTTP upload endpoint uses a different host.