1.0.4 • Published 4 years ago

dao-im-sdk v1.0.4

Weekly downloads
12
License
MIT
Repository
-
Last release
4 years ago

dao-im-sdk guide

Install&Usage (安装&使用)

# Install(安装)
npm install --save dao-im-sdk

# Usage(使用)
//浏览器
<script src="//unpkg.com/dao-im-sdk@1.0.4/index.js"></script>
<script> console.log(imsdk);</script>

//ES6
import imsdk from 'dao-im-sdk'

// commonjs
const imsdk = require('dao-im-sdk')

发送消息格式

{
  "code": "USER_INFO",  # 事件码(必填项)
  "data": "xxxxxxx",    # 发送给服务器数据(aes加密)
  "mes": "获取用户信息" # 操作说明
}

返回消息格式

{
  "code": "USER_INFO",  # 事件码
  "data": "xxxxxxxxx",  # 响应的数据(first为true时使用ras解密,否则aes解密)
  "first": false,       # 是否是创建连接以来第一次响应数据
  "mes": "获取用户信息",# 提示信息(status 为false时可能需要展示给用户看)
  "status": true        # 本次操作状态 true成功,false失败
}

Api 使用指南

imsdk类,IM即时通讯SDK.

new imsdk(url, wsUrl, wsConfig, pingTimeout, pongTimeout, reconnectTimeout, pingMsg, repeatLimit, isEncrypt, theme)

options 传入参数

NameTypeDescription
urlStringhttp链接地址.
wsUrlStringwebsocket链接地址.
wsConfigObjectwebsocket 参数配置.
pingTimeoutNumber未收到消息多少秒之后发送ping请求,默认15000毫秒.
pongTimeoutNumber发送ping之后,未收到消息超时时间,默认10000毫秒.
reconnectTimeoutNumber重连等待时间,默认2000毫秒.
pingMsgString心跳检测默认值,默认ping.
repeatLimitNumber最大重连次数.
isEncryptBoolean是否启用RSA/AES加密(注意:启用需要后端配合开发).
themeObject主题皮肤(窗口配置).

Methods 方法

NameTypeDescription
callback()Objectcallback 操作回调监听(含onmessage、onclose、onopen、onreconnect、onerror等)
close()Object关闭webocket连接
onclose()Objectwebsocket关闭监听
onerror()Objectwebsocket错误监听
onmessage()Objectwebsocket通知消息监听
onopen()Objectwebsocket打开监听
onreconnect()Objectwebsocket断线重连监听
onSend()Objectwebsocket 发送消息方法(传入Object(固定格式的参数))
setAesIv(iv)String设置AES偏移量(默认为ABCDEF1234123412)
setAesKey(key)String设置aes密钥
setRsaPrivateKey(key)String设置rsa私钥
setRsaPublicKey(key)String设置rsa公钥
formatData()Object解析onMessage Data接受的数据(后端websocket推送的内容解密)
formatDataSend()Object编码onSend Data发送的数据(前端websocket推送的内容加密)
getRSAWsConfig()String获取ws配置的RSA加密串(根据wsConfig传入的参数生成RSA加密串)
getRSAWsConfigBase64()String获取ws配置的RSA加密串base64编码(根据wsConfig传入的参数生成RSA加密串(进行BASE64编码))

传入参数

//zd  0:小程序 1:PC 2:手机WEB -1:银行端APP
//lx  1:企业用户, 2:个人用户,3:银行, 4:平台客服
//ws://20.168.1.234:8085/ws?p=(sessionId=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&zd=1&lx=1)
//--------------------------------------------
 var mesMap = {
    PING_PONG: "检测心跳",
    NEW_MESSAGE: "新消息",
    USER_INFO: "获取用户信息",
    READ_MESSAGE: "读消息",
    SEND_MESSAGE: "发送消息",
    UPDATE_KEY: "更新rsa key",
    SERVER_ERROR: "服务器错误",
    HISTORY_LIST: "获取历史会话列表",
    REMOVE_MESSAGE: "移除会话消息",
    RECEIVE_MESSAGE_STATUS: "消息状态",
    HISTORY_MESSAGE_LIST: "获取历史消息列表",
    UNREAD_MESSAGE_TOTAL: "获取该用户未读消息总数",
  };
//初始化实例
let im = new imsdk({
      isEncrypt:true,
      wsUrl:"ws://127.0.0.1:3001",
      wsConfig: {
        zd: -1,
        lx: 1,
        token: "66a33a46-5031-42bd-b624-8460036d28e6",
        t: new Date().getTime(),
      }
  });
im.callback = function (e) {
  console.log("callback监听()=>", e);
  if(e.event==="onmessage"){
    console.log("onmessage=>",e.data)
  }else{
    console.log("callback=>",e)
  }
}

// 初始化更新
if (event === "onmessage" && data.code != "PING_PONG") {
  for (const i in mesMap) {
    if (mesMap.hasOwnProperty(i)) {
      const el = mesMap[i];
      if (data.code === "UPDATE_KEY") {
        im.setAesKey(data.data);
      }
      if (data.code === i) {
        console.log("【" + el + "=>】", data.data || null);
      }
    }
  }
}


# 下面无关紧要的代码
//自动生成事件绑定
 var getId = function (id) {
    return document.getElementById(id);
  };
  var html = "";
  for (const i in mesMap) {
    if (mesMap.hasOwnProperty(i)) {
      const el = mesMap[i];
      html +=
        '<button style="margin-bottom:10px" id="' +
        i +
        '">' +
        el +
        "</button></br>";
    }
  }
  getId("examples").innerHTML = html;
  var btns = getId("examples").querySelectorAll("button");
  for (let i = 0; i < btns.length; i++) {
    btns[i].onclick = function (e) {
      // console.log(e.target.id, e.target.innerHTML);
      var code = e.target.id;
      if(code==="pong"){
        im.onSend("pong");
      }
      im.onSend({
        code: code,
        data: { text: e.target.innerHTML },
        mes: "",
        //user:code!="PING_PONG" && "MqZNRtRIS0K7Al13SbBXSQ==" || "" //无关紧要的代码
      });
    };
  }
1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago