@algonauti/ember-cable

中文文档
2.0.0发布于 2026/10/16MITTypeScriptCommonJS近 30 天下载 2462

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

中文使用文档
文档目录5 节
  1. 安装
  2. 基本用途
  3. 类型脚本
  4. 捐款
  5. 许可证

嵌入式电缆

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

CI Ember Observer Score

页:1 发表于 v2 加词 (英语: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

基本用途

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

// 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来写作,并用自己类型的声明来写出.

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。 。 。 .

捐款

见 捐款 详细指南.

许可证

嵌入式电缆在 MIT 许可证。 。 。 .

版本与发布记录

@algonauti/ember-cable 共发布 9 个版本,首次发布于 2019/07/15;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.0.0latest2026/10/1622 天前
1.4.02022/06/234 年前
1.3.02021/12/024 年前
1.2.02021/04/125 年前
1.1.02021/01/115 年前
1.0.02020/05/056 年前

关于 @algonauti/ember-cable 的常见问题

@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 和提交记录判断。