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

ehs-chat-client

v1.0.0

Published

聊天im 前端 js sdk

Downloads

3

Readme

众望智慧IM服务系统 js-sdk

安装

在浏览器中

直接在html中通过script引用

<script src="/dist/ehsChat.client.js"></script>

在vue中

npm install ehs-chat-client

如何使用

在浏览器中使用

var chatClient = new ehsChatClient('appid', 'token', 'host')
chatClient.onConnected = function() {
  console.log('connected')
}
chatClient.onError = function(error) {
  console.log(error)
}
chatClient.init()

在vue中使用

在main.js中

import EhsChatClient from 'ehs-chat-client'
const whiteRouteList = ['Login']
router.beforeEach((to, form, next) => {
  const userToken = store.getters['user/userToken']
  const appId = store.getters['user/appId']
  const userId = store.getters['user/userId']
  if ((!userToken || !appId || !userId) && whiteRouteList.indexOf(to.name) === -1) {
    next({ name: 'Login' })
  } else {
    if (!Vue.prototype.$ehsChatClient) {
      Vue.prototype.$ehsChatClient = new EhsChatClient(appId, userToken)
    }
    next()
  }
})

在聊天应用入口 比如session.vue中初始化

export default {
  created () {
    const self = this
    self.$ehsChatClient.onError = (error) => {
      console.log(error)
    }
    self.$ehsChatClient.onConnected = () => {
      // do something
    }
    self.$ehsChatClient.init()
  }
}

API

初始化 SDK
var chatClient = new ehsChatClient('appid', 'token', 'host')
chatClient.onConnected = function() {
  console.log('connected')
}
chatClient.onError = function(error) {
  console.log(error)
}
chatClient.init()
socket 对象
const socket = chatClient.socket

socket.io 使用方法请参考官方文档

Methods
  • init()
  • getMessageSessions()

方法说明:获取当前用户的会话列表

chatClient.getMessageSessions().then(sessions => {
  console.log(sessions)
  //do something
})
  • getSessionMessages(sid, limit, lastmsgId)

方法说明:获取会话内的历史消息
参数说明:
sid: 会话ID
limit: 每次请求获取的条数
lastmsgId: 小于次ID的数据,为空则从最新消息开始

chatClient.getSessionMessages(sid, limit, lastmsgId).then(messages => {
  console.log(messages)
  //do something
}).catch(error => {
  console.log(error)
})
  • sendPrivateMessage(message)

方法说明:发送单聊消息
参数说明:
message对象说明

{
    "senderUserId": "当前发送者用户ID",
    "targetId": "接收方用户ID",
    "content": "消息内容 参考消息内容对象说明",
    "messageType": "消息类型 参考消息类型说明"
}

content说明,暂时支持文字和图片消息
1.文字消息

{
  "content": "文字消息"
}

2.图片消息

{
  "content": "图片base64编码",
  "fullUrl": "图片访问路径"
}
chatClient.sendPrivateMessage(message).then(res => {
  //do something
}).catch(error => {
  console.log(error)
})
  • sendGroupMessage(message)

方法说明:发送群聊消息
参数说明:参考单聊消息

  • setMessageSessionRead (sid)

方法说明:设置会话为已读
参数说明:
sid: 会话ID

chatClient.setMessageSessionRead(sid).then(() => {
  //do something
}).catch(error => {
  console.log(error)
})
  • uploadFile({formData, onProgress})

方法说明:上传聊天文件(图片,语音等) 参数说明:
formData:FormData对象,放置上传的文件
onProgress:上传进度回调函数

const formData = new FormData()
formData.append('up_file', File)
chatClient.uploadFile({formData, onProgress (percent) {
  console.log(percent)
}}).then(result => {
  // do something
}).catch(error => {
  console.log(error)
})
属性
  • onError

socket 发生错误时的回调方法
等同于如下事件:

chatClient.socket.on('error', (error) => {})
  • onConnected

socket 连接成功后的回调方法
等同于如下事件:

chatClient.socket.on('connect', () => {})
  • onDisconnected

socket 断开连接的回调方法
等同于如下事件:

chatClient.socket.on('disconnect', () => {})
  • onMessage

socket 接收到消息后的回调方法
等同于如下事件:

chatClient.socket.on('message', (message) => {})
  • isConnected

boolean 判断socket是否已处于连接状态

常量说明
  • socketMessageTypes

socket 消息类型,onMessage 回调得到的数据中去判断使用

module.exports = {
  USER_MESSAGE: 'user' // 用户消息
}

使用说明:

import EhsChatClient from 'ehs-chat-client'
const type = EhsChatClient.socketMessageTypes.USER_MESSAGE
  • messageContentTypes

消息内容类型

// 1 文字消息 2语音消息 3图片消息 4文件消息 5位置消息 6自定义消息
module.exports = {
  TEXT_MESSAGE: 1,
  VOICE_MESSAGE: 2,
  IMAGE_MESSAGE: 3,
  FILE_MESSAGE: 4,
  LOCATION_MESSAGE: 5,
  CUSTOM_MESSAGE: 6
}

使用说明:

import EhsChatClient from 'ehs-chat-client'
const type = EhsChatClient.messageContentTypes.TEXT_MESSAGE
  • conversationTypes

会话类型

module.exports = {
  PRIVATE: 1, // 单聊
  GROUP: 2 // 群聊
}

使用说明:

import EhsChatClient from 'ehs-chat-client'
const type = EhsChatClient.conversationTypes.PRIVATE