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 🙏

© 2026 – Pkg Stats / Ryan Hefner

kfjssdk

v0.1.29

Published

客服im系统jssdk,适配微信小程序和web

Readme

客服IM系统jssdk

客服系统的jssdk,适配微信小程序和web

特征

  • [x] 适配微信小程序和web
  • [x] 支持文本消息、图片消息、文件消息
  • [x] 心跳保活、自动重连、超时断开功能
  • [x] 简单的事件监听api和消息发送api
  • [x] 无业务入侵,只提供消息传递层
  • [ ] 群组聊天(未实现)

使用方法

web使用

  • 安装 npm i kfjssdk

微信小程序使用

  • 在发布页下载 微信小程序sdk 到项目中,然后使用

jssdk API

Im方法

封装了ws连接的门面方法,返回Ws类的实例 提供了以下方法

  • login 登录到im后台服务
  • on 注册事件监听
  • send 发送消息
  • disconnected 断开连接

获取到Im并初始化示例代码

import {Im} from "jssdk";

// Im门面方法使用了单例设计模式,全局只需要初始化一次
const im = Im("ws://客服系统ws地址" || {
  url: "ws://客服系统ws地址",
  timeout: 30, //单位秒,默认30秒
  retryTimes: 10, // 重连次数 默认10次
},"web");

// 当ws连接成功后,第二次调用,可以直接获取实例
const instance = Im(); 

Im(params).login(appId, signature, userId) 方法返回一个 promise

  • 参数appId当前ws所属的应用的id根据后台来
  • 参数signature一个连接的签名,一般是配合后台调接口获得
  • 参数userId当前要连接到ws的用户
import {Im} from "kfjssdk";

const instance = Im("ws://客服系统ws地址");
instance.login("appId", "signature", "userId")
  .then(res => {
    /*处理登录成功后的逻辑*/
  })
  .catch((err) => {
    /*处理失败后的逻辑*/
    console.error(err);
  });

Im(params).on(事件名称,(数据) => {},"event name") 返回事件id 注册事件回调

事件有以下几种事件类型

  • Events.text 当发送的消息是文本消息时触发该事件
  • Events.face 当发送的消息是表情消息时触发该事件
  • Events.file 当发送的消息是文件消息时触发该事件
  • Events.image 当发送的消息是图片消息时触发该事件
  • Events.system 当有系统消息时触发该事件
  • Events.error 当连接有错误时触发该事件
  • Events.kickout 当有被系统剔除登录时触发该事件
  • Events.bot 当有机器人消息时触发该事件
  • Events.unknown 当有未知的会话类型时触发该事件
  • Events.close 当有ws关闭时触发该事件
  • Events.status 当消息发送成功或失败时系统通知该消息状态时触发该事件

绑定方法如下所示

import {Events} from "kfjssdk/lib/events";
import {Im} from "kfjssdk";
// 此时假设已经login过了。由于是单例模式,所以直接返回实例
// event name 相同时只会绑定一次,如果不传入event name默认
const im = Im();
im.on(Events.text, data => {
});
// 这里同时注册text事件,会同时生效
im.on(Events.text, data => {
},"event name");
im.on(Events.image, data => {
},"event name");
im.on(Events.file, data => {
},"event name");
im.on(Events.status, data => {
},"event name");

// ....

Im().off(事件id) 取消事件的绑定

import {Im} from "./index";
import {Events} from "./events";

const im = Im()

const evtId = im.on(Events.Text,(data:any) => {})

// 取消事件监听
im.off(evtId)

Im().send(消息体) 返回值是 promise

参数消息体是由Message类生成的。

class: Message 有以下方法
  • constructor: new Message({to:"发送给谁(必须)",{from: "发送人(必须)"}})
  • new Message({}).Text({text:"文本消息"})
  • new Message({}).Image({uuid:"uuid",imageInfoArray:["图片信息"])
  • new Message({}).File({uuid, fileName, fileUrl, fileSize})
  • new Message({}).Face({ index: "表情索引,用户自定义", data: "额外数据非必须"})
  • new Message({}).Custom({ data: "自定义消息的 data 字段", description: "自定义消息的 description 字段", extension: " 自定义消息的 extension 字段"})
  • new Message({}).setCustomData(data: NonNullable) 设置自定义消息 更多方法定义如下
generate(): string;
message(): import("./types").MessageBody;
flow(flow: MessageFlow): this;
status(status: MessageStatus): this;
conversationType(t: ConversationType): this;
conversationID(id?: string): this;
Text(payload: import("./types").MessageText): this;
Image(payload: import("./types").MessageImage): this;
File(payload: import("./types").MessageFile): this;
Custom(payload: import("./types").MessageFile): this;
Face(payload: import("./types").MessageFace): this;
Bot(payload: import("./types").MessageBot): this;

使用方法如下

import {Im, Message} from "kfjssdk";
import {ConversationType} from "kfjssdk/lib/types";

const im = Im();
const msg = new Message({to: "demo", from: "fuyoo",platform:"web"})
  .conversationType(ConversationType.C2C) // 这里必须把会话类型设置为C2C
  .Text({text: "this is a text message"});
im.send(msg)
  .then((msg) => {
    /*做发送成功的逻辑*/
  })
  .catch(error => {
    console.trace(error);
  });

关于服务器API,请查看 api文档 如果打不开请联系管理员