# jarvis-replica

> Jarvis受控端

Latest version **0.2.5** (published 2019-12-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install jarvis-replica
pnpm add jarvis-replica
yarn add jarvis-replica
bun add jarvis-replica
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.5 |
| Published | 2019-12-26 |
| First published | 2019-11-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 13.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | paopao.m |
| Keywords | Replica |

## Links

- npm: https://www.npmjs.com/package/jarvis-replica
- npm.io page: https://npm.io/package/jarvis-replica

## Dependencies (2)

- [jarvis-util](https://npm.io/package/jarvis-util.md) ^0.2.4
- [jarvis-types](https://npm.io/package/jarvis-types.md) ^0.2.3

## Recent versions

- 0.2.5 (latest) — 2019-12-26
- 0.2.4 — 2019-12-25
- 0.2.3 — 2019-12-25
- 0.2.2 — 2019-12-19
- 0.2.0 — 2019-12-19
- 0.1.2 — 2019-12-16
- 0.1.1 — 2019-12-16
- 0.1.0 — 2019-12-16
- 0.0.5 — 2019-12-10
- 0.0.4 — 2019-11-25
- 0.0.3 — 2019-11-25
- 0.0.2 — 2019-11-25
- 0.0.1 — 2019-11-25

## README

# Jarvis Replica

Jarvis 大屏受控端, [查看演示项目](https://kqe8o-1234.sse.codesandbox.io/)

## 安装

```shell
npm install jarvis-replica
```

## 使用

```javascript
import JarvisReplica from 'jarvis-replica'

const replica = new JarvisReplica({
  url: `ws:127.0.0.1:3000`,
  secrets: '5r9b4vto246doa9hwk55rr',
  id: 'vis-1'
})

// 连接服务器
replica.connect()
```

| 参数名     | 说明                                | 类型     | 必填   |
|-----------|------------------------------------|---------|---------|
| url       | Jarvis 服务器地址                    | `string` | true  |
| secrets   | 服务器设置的密码                      | `string` | true  |
| id        | 受控端的唯一ID                       | `string` | false  |

## 监听事件

你可以使用 `on` 方法监听事件：

```javascript
replica.on('open', (e) => {
  console.log('已经连接到控制服务器!')
})

replica.on('command', (e) => {
  console.log('接收自定义事件')
})

replica.on('*', data => {
  console.log('接收所有的自定义事件')
})
```
目前支持的事件如下：

| 事件名     | 说明                                |              参数                  |
|-----------|------------------------------------|---------------------------------- |
| open      | 连接到控制服务器                      | -                                 |
| error     | 连接出错                             | -                                 |
| close     | 连接断开                             | -                                 |
| reconnect-fail     | 重连失败                    | -                                 |
| *         | 所有自定义 `command` 事件             | `{command: 'type', arguments: {}}`|
|[ command ]| 自定义事件                           | 参数对象                           |

## 心跳检测
服务端每隔 `10s` 会向受控端发送 `ping` 事件, 受控端返回 `pong`, 若检测到断开连接, 则每隔 `10s` 尝试重连一次, 连续三次重连失败, 受控端会发送 `reconnect-fail` 事件给页面, 后续逻辑需要监听事件自行处理

```JavaScript
replica.on('reconnect-fail', () => {
  console.log('replica跟server断开连接, 并尝试重连失败')
})
```

## 其他

目前受控端支持接收来自三种方式发送的指令, 具体使用方式请参考相关文档

  - `controller.send` 发送的指令
  - 直接通过服务端, 使用 `server.sendToReplica` 发送的指令
  - 其他server通过http发送的指令

服务端和控制端的使用方式, 可查阅下方文档

[Jarvis 服务端地址](https://www.npmjs.com/package/jarvis-server)

[Jarvis 控制端地址](https://www.npmjs.com/package/jarvis-controller)

[Jarvis 演示项目代码地址](https://codesandbox.io/s/jarvis-demo-kqe8o?fontsize=14&hidenavigation=1&theme=dark)

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