0.1.46 • Published 4 years ago
im-sdk-ts v0.1.46
IM sdk (有消息前端IM部分SDK)
主要功能
- 连接远程 websocket 服务器并监听
- 用户连接成功/失败的回调
- 发送/接收消息
- 开启或关闭日志
- 主动断开连接(不重连)
- 连接断开或错误 自动重连
- 重连最多 3 次 重连间隔 3s
- 重连成功/失败的回调
- 也可手动重连
- 与服务器端的心跳保持 间隔 1 分钟
- 账号在其他地方登录(如 APP)的回调
- 网络变化(如断线、重新连接)的回调
- 发送/接收消息时的类型定义常量
本地环境
- node版本建议大于10.0
- typescript版本建议大于3.7
注:编辑器需要安装eslint、prettier等常用格式化工具
命令
- 本地开发
npm start
- 打包
npm run build
Usage 使用
import { imConnect, imEventInstance, WebsocketEvent } from 'im-sdk-ts';
// 获取IM实例
const im = imConnect(url, {loginUserId: 'xx', loginToken: 'xx'});
// 获取IM事件订阅器实例
const imEvent = imEventInstance();
im.setDebugEnable(true);
im.setDebugPingPongEnable(false);
imEvent.socketOn(WebsocketEvent.EVENT_LOG, content => {
console.log(content);
});
imEvent.socketOn(WebsocketEvent.EVENT_RECONNECT_SUCCESS, () => {
console.log('掉线重连成功了');
});
// 建立连接
im.connectAndListen()
// 发送单聊
im.send121Msg(msg)
// 断开连接
im.disconnect()
Example
详细例子见:examples/src/pages/index.tsx
onIMData 消息回调的详细文档 见消息文档
ImSdk实例方法
实例方法 | 说明 |
---|---|
setDebugEnable | 是否打印日志 |
setDebugPingPongEnable | 是否打印心跳日志 |
connectAndListen | 连接服务器,监听 socket 连接状态 |
send121Msg | 发送单聊信息 |
sendGroupMsg | 发送群聊信息 |
disconnect | 主动断开连接 不会自动重连 |
tryReconnect | 手动尝试重连 |
isUserLogined | 用户是否已登录 不管以后是否掉线 |
isUserOnline | 用户是否在线 |
getLoginInfo | 获取用户授权时的信息 |
发布
执行npm publish
参考
脚手架
feature
- Package webSocket and define Porotocal that equal server side
- Written in TypeScript with predictable static types.
- Provide some function change environment variable for debug,etc: setDebugEnable(),setDebugPingPongEnable().
- Use JEST for unit testing
0.1.46
4 years ago
0.1.45
4 years ago
0.1.44
4 years ago
0.1.43
4 years ago
0.1.41
4 years ago
0.1.42
4 years ago
0.1.40
4 years ago
0.1.39
4 years ago
0.1.38
4 years ago
0.1.37
4 years ago
0.1.36
4 years ago
0.1.35
4 years ago
0.1.33
4 years ago
0.1.34
4 years ago
0.1.32
4 years ago
0.1.31
4 years ago
0.1.30
4 years ago
0.1.29
4 years ago
0.1.28
4 years ago
0.1.26
4 years ago
0.1.27
4 years ago
0.1.24
4 years ago
0.1.22
4 years ago
0.1.23
4 years ago
0.1.21
4 years ago
0.1.20
4 years ago
0.1.19
4 years ago
0.1.18
4 years ago
0.1.16
4 years ago
0.1.17
4 years ago
0.1.15
4 years ago
0.1.12
4 years ago
0.1.13
4 years ago
0.1.14
4 years ago
0.1.6
4 years ago
0.1.5
4 years ago
0.1.4
4 years ago
0.1.3
4 years ago
0.1.2
4 years ago
0.1.1
4 years ago
0.1.0
4 years ago