# dd-lv-interaction-paas-jssdk

> 统一 LIVE INTERACTION SDK

Latest version **0.0.1** (published 2021-03-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install dd-lv-interaction-paas-jssdk
pnpm add dd-lv-interaction-paas-jssdk
yarn add dd-lv-interaction-paas-jssdk
bun add dd-lv-interaction-paas-jssdk
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2021-03-17 |
| First published | 2021-03-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 10 |
| Unpacked size | 75.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | zhangfa.zf |

## Links

- npm: https://www.npmjs.com/package/dd-lv-interaction-paas-jssdk
- npm.io page: https://npm.io/package/dd-lv-interaction-paas-jssdk

## Dependencies (10)

- [uuid](https://npm.io/package/uuid.md) ^8.3.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.20
- [@types/uuid](https://npm.io/package/@types/uuid.md) ^8.3.0
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.159
- [serialize-error](https://npm.io/package/serialize-error.md) ^7.0.1
- [@ali/lwp-over-ws](https://npm.io/package/@ali/lwp-over-ws.md) 2.3.7
- [@ali/ding-mediaid](https://npm.io/package/@ali/ding-mediaid.md) ^2.2.2
- [@ali/dingtalk-idl-ts](https://npm.io/package/@ali/dingtalk-idl-ts.md) ^4.2.2
- [@ali/dingtalk-lwp-sdk](https://npm.io/package/@ali/dingtalk-lwp-sdk.md) 2.2.4
- [@ali/dingtalk-idl-parser](https://npm.io/package/@ali/dingtalk-idl-parser.md) ^3.0.2

## Recent versions

- 0.0.1 (latest) — 2021-03-17
- 0.0.1-alpha.1 — 2021-03-17

## README

# 钉钉直播互动JSSDK介入文档

## 环境配置

* 使用 npm 或 yarn 安装`dd-lv-interaction-paas-jssdk`

```bash
npm install --save dd-lv-interaction-paas-jssdk
yarn add dd-lv-interaction-paas-jssdk
```

## 工程接入

### 公共部分

* 引入`interactionSdk`

  ```typescript
  import { interactionSdk } from 'dd-lv-interaction-paas-jssdk';
  ```

* 登录认证

  ```typescript
  const Setting = { /* content */ }

  interactionSdk.getSettingService().set(Setting);

  interactionSdk.getAuthService().login();
  ```

  其中setting内容如下：

  ```typescript
    interface Setting {
      environment: 0, // 环境
      wsUrl: string; // websocket url
      appKey: string; // 应用key IM_PAAS分配给应用的key
      mediaHost: string; // 多媒体上传服务器地址
      deviceId: string; // 设备唯一id
      appName?: string; // app_name 应用名称
      appVersion?: string; // 应用版本号
      appLocale?: string; // App 语言区域（默认zh_CN）
      osName?: string; // 操作系统名称，如Win、macOS、iOS、Android等
      osVersion?: string; // 操作系统版本
      deviceName?: string; // 设备名称
      deviceType?: string; // 设备型号
      deviceLocale?: string; // 设备语言（默认zh_CN）
      timeZone?: string; // time_zone 时区（默认Asia/Shanghai）
      subscribeServerPush?: string // subscribe-server-push header
      log: ILogConfig; // log回调
      authTokenCallback(): Promise<Token>; // on_callback 获取登录token的回调函数（注意需要做成异步操作，不能有阻塞）
    };

    interface ILogConfig {
      info(...args: any[]): void;
      error(...args: any[]): void;
    }
  ```

### 服务部分

#### 互动API服务

使用`interactionSdk.getInteractionService()`获取互动API服务实例。

*在JSSDK层面，各个实例是单例的。*

* 进入房间
  ```typescript
    iinteractionSdk
      .getInteractionService()
      .enterRoom({
        roomId: 'roomId',
        nick: 'nickname'
      }).then(res => {
        if (res.success) console.log('进入房间成功')
        else throw new Error('res error')
      }).catch(err => {
        console.error(err, '进入房间失败')
      })
  ```
* 查询房间详情
  ```typescript
    interactionSdk
      .getInteractionService()
      .getRoomDetail({
        roomId: 'roomId'
      }).then((res) => {
        if (res.roomDetailVO) console.log('查询房间信息成功')
        else throw new Error('res error')
      }).catch(err => {
        console.error(err, '查询房间信息失败')
      })
  ```
* 发布房间公告
   ```typescript
    interactionSdk
      .getInteractionService()
      .publishRoomNotice({
        roomId: 'roomId',
        notice: '房间公告'
      }).then((res) => {
        if (res.success) console.log(`公告设置成功`)
        else throw new Error('res error')
      }).catch(err => {
        console.error(err, '公告设置失败')
      })
  ```
* 离开房间
   ```typescript
    interactionSdk
      .getInteractionService()
      .leaveRoom({
        roomId: 'roomId'
      }).then((res) => {
        if (res.success) console.log(`离开房间成功`)
        else throw new Error('res error')
      }).catch(err => {
        console.error(err, '离开房间失败')
      })
  ```
* 点赞
   ```typescript
    interactionSdk
      .getInteractionService()
      .sendLikesMsg({
        roomId: 'roomId',
        count: 1 // 允许几秒内发送一次弹幕
      }).then((res) => {
        if (res.interval || res.interval === 0) console.log('点赞成功')
        else throw new Error('res error')
      }).catch(err => {
        console.error(err, '点赞失败')
      })
  ```
* 发文本消息
   ```typescript
    interactionSdk
      .getInteractionService()
      .sendCommentMsg({
        roomId: 'roomId',
        content: '消息内容',
        type: 1, // 弹幕的类型,比如普通弹幕、超级弹幕,由调用方决定,服务端透传
        extension: {}
      }).then((res) => {
        if (res.commentId) console.log('消息发送成功')
        else throw new Error('res error')
      }).catch(err => {
        console.error(err, '消息发送失败')
      })
  ```
* 发自定义消息
   ```typescript
    interactionSdk
      .getInteractionService()
      .sendCustomMsg({
        roomId: 'roomId',
        subType: 100001,
        body: '消息内容', // 消息子类型,业务方决定
        receivers: [], // 接受者,如果不传默认给房间内全部在线用户广播
        priority: 0 // 自定义消息的优先级, 目前只支持高中低三个, 对应100:高, 0:中, -100低, 不传默认为0, 即中优先级
      }).then((res) => {
        if (res.messageId) console.log('发送消息成功')
        else throw new Error('res error')
      }).catch(err => {
        console.error(err, '发送消息失败')
      })
  ```
* 查询消息
   ```typescript
    interactionSdk
      .getInteractionService()
      .getRoomDetail({
        roomId: 'roomId'
      }).then((res) => {
        if (res.messages) {
          console.log('查询消息成功')
          res.messages.forEach(item => {
            // 部分信息
            console.log(`id: ${item.commentId}, nick: ${item.nick}, content: ${item.content}`)
          })
        } else throw new Error('res error')
      }).catch(err => {
        console.error(err, '查询消息失败')
      })
  ```
#### 事件服务

使用`interactionSdk.getEventService()`获取事件实例。

* 注册监听事件

```typescript
interactionSdk.getEventService().register((eventName, data) => {
  /* do something */
})
```

* 事件列表

```typescript
enum EventNameEnum {
  PaaSConnectionChanged = 'paas.connection.changed', // 登录状态
  PaaSLiveRoomEnter = 'paas.live.room.enter', // 进入房间状态
  PaaSLiveRoomOlineCount = 'paas.live.room.onlinecount', // 在线人数变更
  PaaSLiveRoomNotice = 'paas.live.room.notice', // 设置公告
  PaaSLiveRoomComment = 'paas.live.room.comment', // 发布消息
  PaaSLiveRoomLike = 'paas.live.room.like', // 收到点赞
  PaaSLiveRoomCustomMessage = 'paas.live.room.customMessage', // 发布自定义消息
}
```

* 事件解绑

```typescript
interactionSdk.getEventService().unRegister()
```

## 浏览器环境要求

jssdk 对浏览器环境有一定的兼容性要求，主要依赖是对 `websocket` 的兼容性要求。兼容性参考表如下：

| 主流浏览器      | 最低版本要求 |
| :-------------- | :----------- |
| Chrome          | 4 ~ 88 +     |
| Firefox         | 11 ~ 85 +    |
| IE              | 10 +         |
| Edge            | 12 ~ 88 +    |
| Opera           | 12.1 ~ 72 +  |
| Safari          | 5 ~ 13.1 +   |
| Android Browser | 89 +          |
| Safari for iOS  | 4.2 ~ 13.7 + |

更多兼容性请参考：https://caniuse.com/?search=websocket

---
_Source: https://npm.io/package/dd-lv-interaction-paas-jssdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
