GithubHelp home page GithubHelp logo

zysyyz / kim_web_sdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from klintcheng/kim_web_sdk

0.0 0.0 0.0 2.95 MB

kim web sdk

License: Apache License 2.0

Shell 0.29% JavaScript 0.10% TypeScript 99.60%

kim_web_sdk's Introduction

kim_web_sdk

本项目是King IM CLoud的web版本SDK。

安装

npm i kingim

Quick Start

我们再次回顾下客户端的生命周期状态图,在SDK端所有逻辑都离不开这个状态图:

sdk_status.png

一个简单的调用流程如下:

const tags = ["web"]
// 1. 初始化
let cli = new KIMClient(gatewayURL, { token: tokens[0], tags });

// 2. 注册监听器
let evts = [KIMEvent.Closed, KIMEvent.Reconnecting, KIMEvent.Reconnected, KIMEvent.Kickout]
cli.register(evts, eventcallback)
cli.onmessage(messagecallback)
cli.onofflinemessage(offmessagecallback)

// 3. 登录
let { success, err } = await cli.login()
if (!success) {
    log.error(err)
    return
}

// 4. 发送消息
let { status, resp, err: err2 } = await cli.talkToUser("test2", new Content("hello"))
if (status != Status.Success) {
    log.error(err)
    return
}
log.info(`resp - ${resp?.messageId} ${resp?.sendTime.toString()}`)

await sleep(10)

// 5. 登出
await cli.logout()

我们主要了解下其中三个监听器

  • eventcallback:事件监听器回调方法。在Client的生命周期图中,状态随着主动或被动的事件触发而发现改变。比如断线重连时两种状态ReconnectingReconnected,虽然SDK可能在几秒内就自动重连成功,但是这个变动过程还是要通知给上层,让业务决定是否显现给用户。
let eventcallback = (evt: KIMEvent) => {
    log.info(`event ${evt}`)
};
  • messagecallback:用于接收在线消息的回调方法。
let messagecallback = (m: Message) => {
    log.info(m)
}
  • offmessagecallback:离线消息回调方法。通常是dologin成功并且同步离线索引完成之后,才会回调给上层,如下是调用示例:
let offmessagecallback = (om: OfflineMessages) => {
    // 离线时的发送方用户列表
    let users = om.listUsers()
    if (users.length > 0) {
        log.info(`offline messages from users of ${users}`)
        // lazy load the first page messages from 'users[0]'
        let messages = om.loadUser(users[0], 1)
        log.info(messages)
    }
    // 离线的群列表
    let groups = om.listGroups()
    if(groups.length > 0) {
        log.info(`offline messages from groups of ${groups}`)
    }
}
  • om.loadUser(users[0], 1) 表示加载users[0]的第一页离线消息。

kim_web_sdk's People

Contributors

klintcheng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.