npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kingim-ty

v1.1.4

Published

king im sdk

Downloads

5

Readme

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]的第一页离线消息。