# @efox/chatroom

> chatroom工具库

Latest version **1.24.2** (published 2020-06-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @efox/chatroom
pnpm add @efox/chatroom
yarn add @efox/chatroom
bun add @efox/chatroom
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.24.2 |
| Published | 2020-06-18 |
| First published | 2020-06-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 68.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Simon |
| Maintainers | ckken, doerme, hiitiger, hupp, linhaoran, lucky-dog, magic_zhong, nijina, tim2018, wangcylive, wu_wei, yangshangzhi, yz101x, zhengy1995 |

## Links

- npm: https://www.npmjs.com/package/@efox/chatroom
- Repository: https://git.yy.com/webs/efox/middle-platform/efox-im-middleware
- Homepage: https://github.com/simonwong/fly-helper#readme
- Issues: https://github.com/simonwong/fly-helper/issues
- npm.io page: https://npm.io/package/@efox/chatroom

## Dependencies (1)

- [hummer-chatroom-svc-sdk](https://npm.io/package/hummer-chatroom-svc-sdk.md) ^2.1.4

## Recent versions

- 1.24.2 (latest) — 2020-06-18
- 1.21.2 — 2020-06-12
- 1.21.0 — 2020-06-10
- 1.18.0 — 2020-06-10
- 1.13.0 — 2020-06-10
- 1.12.0 — 2020-06-10

## README

# 聊天室封装简易sdk

## 创建实例(createChatroom)

```javascript
import createChatroom from '@efox/chatroom'
const chatromm = await createChatroom(options)
```

请求参数：Object

| Name      | Type                    | Description                                         |
| --------- | ----------------------- | --------------------------------------------------- |
| hummer?   | Hummer                  |     原始hummer（没传会自动创建）                                      |
| otp       | string                  |     用户token（没传原始hummer时，必须传）               |
| appid?    | string                  |     应用appid（没传原始hummer时，必须传）           |
| region?    | string                  |    用户区域（没传原始hummer时，必须传）              |
| roomid?    | string                  |     房间id（没传会自动创建）                                  |
| extListenEvents?    | array                  |     补充监听事件列表                          |

响应数据：Object

| Name      | Type   | Description                 |
| --------- | ------ | --------------------------- |
| chatromm   | Object | 房间实例                 |


## 实例属性(attributes)

```javascript
const {roomid, root, hummer} = chatromm
```

| Name      | Type                    | Description                                         |
| --------- | ----------------------- | --------------------------------------------------- |
| roomid   | string                  |     房间id                                     |
| hummer    | object                  |     原始hummer          |
| root       | object                  |     原始hummer创建的原始chatroom实例              |

## 发送消息(send)

```javascript
chatromm.send({
    event: '',
    data: {
        content: ''
    }
})
```

请求参数：Object

| Name      | Type                    | Description                                         |
| --------- | ----------------------- | --------------------------------------------------- |
| event   | string                  |     发送事件类型                                   |
| data       | Object                  |    发送数据               |

响应数据：Object

| Name                  | Type              |  Description |
| --------------------- |   --------------  |  ----------  |
| rescode               |      number       | 0：表示成功   |
| msg                   | string            | 返回描述      |


### 发送消息事件类型
#### sendGroupMessage发送群组消息
请求参数：

| Name                  | Type              |  Description |
| --------------------- | ----------------- |   ----------------- |
| content               | string            | 内容            |
| kvExtra               | {[k: string]: string}  | 扩展字段key-value |


响应数据：

| Name                  | Type              |  Description |
| --------------------- | ----------------- | ---------- |
| rescode               |      number       | 0：表示成功   |
| msg                   | string            | 返回描述      |

示例：
```javascript
  const await res = chatromm.send({
    event: 'sendGroupMessage',
    data: {
        content,
        kvExtra
    }
  })
```

#### sendSingleUserMessage发送单播消息
请求参数：

| Name                  | Type              |  Description |
| --------------------- | ----------------- |   ----------------- |
|    content            |     string           | 内容 |
|    receiver           |     string           | 接收者uid |
|    kvExtra            | {[k: string]: string} | 扩展字段key-value |

响应数据：

| Name                  | Type              |  Description |
| --------------------- |   --------------  |  ----------  |
| rescode               |      number       | 0：表示成功   |
| msg                   | string            | 返回描述      |


示例：
```javascript
  const await res = chatromm.send({
    event: 'sendSingleUserMessage',
    data: {
        content, receiver, kvExtra
    }
  })
```

#### sendTextChat发送公屏
请求参数：

| Name                  | Type              |  Description |
| --------------------- | ----------------- |   ----------------- |
|    chat                |     string           | 内容 |
|    extra          |     string          |  |
|    kvExtra        |     {[k: string]: string}           |  |


响应数据：

| Name                  | Type              |  Description |
| --------------------- | ----------------- |  ---------  |
| rescode               | number            | 0：表示成功   |
| msg                   | string            | 返回描述      |

【注】
（1）新业务接入，扩展字段值请使用kvExtra，不要使用extra；
（2）如果是需要与移动端sdk进行互通的老业务，需要使用extra的情形，请与移动端SDK进行互通联调；


示例：
```javascript
  const await res = chatromm.send({
    event: 'sendTextChat',
    data: {
        chat, extra, kvExtra
    }
  })
```



## 订阅所有事件(subscribe)

```javascript
const unsubscribe = chatromm.subscribe(({name, res}) => {
    console.log({
        name, res
    })
})
// 如果要取消该订阅
unsubscribe()
```

请求参数：Function
可以订阅多次，最多限制20次，每次的传入函数，会有以下对象数据：

| Name      | Type                    | Description                                         |
| --------- | ----------------------- | --------------------------------------------------- |
| name   | string                  |     事件类型                                   |
| res       | Object                  |    返回数据               |

响应数据：Function

执行返回函数，可以取消该订阅

### 订阅后可以获取到的事件消息
接受事件类型，请参考以下文档中的接受消息部分中的事件名和返回数据

https://github.com/cherishman2005/hummer-js-sdk-api/blob/master/hummer3.0/chatroom/chatroom%E5%AF%B9%E5%A4%96%E6%8E%A5%E5%8F%A3.md

#### 例如接收公屏消息（TextChat）
```javascript
const unsubscribe = chatromm.subscribe(({name, res}) => {
    if (name === 'TextChat') {
        const {extra, uid, kvExtra} = res
        console.log(res)
    }
})
```

## 订阅所有消息(subscribeMsgs)
只订阅单播、群组、公屏三种消息事件

```javascript
const unsubscribe = chatromm.subscribeMsgs(({name, res}) => {
    // 
})
// 如果要取消该订阅
unsubscribe()
```

请求参数：Function

| Name      | Type                    | Description                                         |
| --------- | ----------------------- | --------------------------------------------------- |
| name   | string                  |     事件类型                                   |
| res       | Object                  |    返回数据               |

响应数据：Function
执行返回函数，可以取消该订阅

## 获取用户列表(getUsers)
请求参数：object

| Name                  | Type              |  Description |
| --------------------- | ----------------- |   ----------------- |
|    num?                |     number           | 数量（默认10） |
|    pos?          |     number          | 起始点（默认0） |

响应数据：string[]

| Name                  | Type              |  Description |
| --------------------- | ----------------- |  ---------  |
| uids               | string[]            | 用户列表   |


## 原始sdk链接
https://github.com/cherishman2005/hummer-js-sdk-api/blob/master/hummer3.0/chatroom/chatroom%E5%AF%B9%E5%A4%96%E6%8E%A5%E5%8F%A3.md

---
_Source: https://npm.io/package/@efox/chatroom · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
