# @algonauti/ember-cable 中文文档

规范地址：https://npmcn.com/package/%40algonauti/ember-cable/

版本：2\.0\.0
npm 元数据更新时间：2026-10-16T08:46:40.245Z
中文译文更新时间：2026-11-06T18:39:37.917Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40algonauti/ember-cable
原文对照：https://npmcn.com/package/%40algonauti/ember-cable/?lang=original
项目主页：https://github.com/algonauti/ember-cable
源码仓库：https://github.com/algonauti/ember-cable

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：15.3 kB，17 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>= 20\.19
- 运行依赖：4 个
- 首次发布：2019-07-15T08:05:28.674Z
- 最新版本发布：2026-10-16T08:46:40.041Z
- 近 30 天下载量：2462（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：9（近 12 个月 1 个）

## 中文简介

用可操作程序为您的 ember.js 应用程序提供动力

## 安装

```sh
npm install @algonauti/ember-cable
```

也可以使用：`pnpm add @algonauti/ember-cable`、`yarn add @algonauti/ember-cable`、`bun add @algonauti/ember-cable`

## README

# 嵌入式电缆

这种加法使得铁路行动有线简便地集成到Ember app.

[![CI](https://github.com/algonauti/ember-cable/workflows/CI/badge.svg)](https://github.com/algonauti/ember-cable/actions) [![Ember Observer Score](https://emberobserver.com/badges/-algonauti-ember-cable.svg)](https://emberobserver.com/addons/@algonauti/ember-cable)

> **页:1** 发表于 [v2 加词](https://github.com/embroider-build/embroider/blob/main/docs/v2-faq.md) (英语:Embroider format). 这需要 `ember-auto-import` (原始内容存档于2018-10-29). QQ 2 and Ember QQ 4.12. 仓库布局 : `ember-cable/` 和 `test-app/` (测试).

### 安装

从您的 ember- cli 工程中运行以下命令 :

```
ember install @algonauti/ember-cable
```

## 基本用途

一旦加通被安装,电缆服务可以在任何地方注入 在申请中需要.

```js
// test-app/app/components/notification-messages/component.js
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { debug, inspect } from '@ember/debug';
import EmberObject from '@ember/object';
import { getOwner } from '@ember/application';

export default class NotificationMessagesComponent extends Component {
  @service cable;
  @service notification;

  constructor() {
    super(...arguments);
    this._setupConsumer();
  }

  _setupConsumer() {
    let consumer = this.cable.createConsumer('ws://localhost:4200/cable');

    consumer.createSubscription('BroadcastChannel', {
      connected() {
        debug('BroadcastChannel#connected');
        this.perform('ping');
      },
      received(data) {
        debug( "received(data) -> " + inspect(data) );
      },
      disconnected() {
        debug("BroadcastChannel#disconnected");
      }
    });

    // Passing Parameters to Channel
    let subscription = consumer.createSubscription({ channel: 'BroadcastChannel', room: 'BestRoom' }, {
      connected() {
        this.perform('ping', { foo: 'bar' });
      },
      received: (data) => {
        this._updateRecord(data);
      },
      disconnected: () => {
        this.notification.notify("BroadcastChannel#disconnected");
      }
    });

    // Using mixin and inject your services:
    let subscriptionHandler = EmberObject.extend({
      notification2: service('notification'),

      connected() {
        this.notification2.notify("subscriptionHandler#connected");
      },
    }).create(getOwner(this).ownerInjection());

    consumer.createSubscription({ channel: 'BroadcastChannel' }, subscriptionHandler);

    setTimeout(() => {
      subscription.perform('ping', { foo: 'bar' });
    }, 3000);

    setTimeout(() => {
      this.cable.destroy();
    }, 9000);

  }

  _updateRecord(data) {
     debug( "updateRecord(data) -> " + inspect(data) );
   }
}
```

## 类型脚本

附加语用TypeScript来写作,并用自己类型的声明来写出.

```ts
import Component from '@glimmer/component';
import { service } from '@ember/service';
import type CableService from '@algonauti/ember-cable/services/cable';

export default class Notifications extends Component {
  @service declare cable: CableService;

  constructor(owner: unknown, args: object) {
    super(owner, args);

    const consumer = this.cable.createConsumer('ws://localhost:4200/cable');

    consumer.createSubscription('BroadcastChannel', {
      connected() {
        this.perform('ping');
      },
      received(data: unknown) {
        console.log(data);
      },
    });
  }
}
```

`Consumer`, (中文(简体) ), `CableService` 和 行动电缆类型`BaseMixin`, (中文(简体) ), `ChannelNameWithParams`, (中文(简体) ), `Subscription`, (中文(简体) ), `Subscriptions`也从 `@algonauti/ember-cable`。 。 。 .

## 捐款

见 [捐款](https://npmcn.com/package/%40algonauti/ember-cable/CONTRIBUTING.md) 详细指南.

## 许可证

嵌入式电缆在 [MIT 许可证](http://www.opensource.org/licenses/MIT)。 。 。 .

## 常见问题

### @algonauti/ember-cable 是什么？

用可操作程序为您的 ember\.js 应用程序提供动力

### 如何安装 @algonauti/ember-cable？

在项目目录运行 npm install @algonauti/ember-cable，或使用 pnpm add / yarn add / bun add。

### @algonauti/ember-cable 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @algonauti/ember-cable 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @algonauti/ember-cable 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>= 20\.19。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @algonauti/ember-cable 可以商用吗？

@algonauti/ember-cable 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @algonauti/ember-cable 的下载量有多少？

根据 npm 官方下载统计，@algonauti/ember-cable 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,462 次，日均约 82 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @algonauti/ember-cable 还在维护吗？

最新版本 2\.0\.0 发布于 2026/10/16（22 天前）；至今共发布 9 个版本，其中过去 12 个月发布了 1 个；首次发布于 2019/07/15。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

