# @feathersjs/socket-commons 中文文档

规范地址：https://npmcn.com/package/%40feathersjs/socket-commons/

版本：3\.1\.5
npm 元数据更新时间：2026-03-19T00:52:55.406Z
中文译文更新时间：2026-11-14T12:53:44.547Z
软件包许可：MIT

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

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

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

- 模块格式：CommonJS
- TypeScript 类型：未声明
- Node.js 要求：\>= 6
- 运行依赖：4 个
- 首次发布：2017-10-21T21:13:06.565Z
- 最新版本发布：2017-12-12T19:46:18.298Z
- 近 30 天下载量：6211（2026-10-15 至 2026-11-13，npm 官方下载统计）
- 已发布版本数：10（近 12 个月 0 个）

## 中文简介

Websocket 提供者共享功能

## 安装

```sh
npm install @feathersjs/socket-commons
```

也可以使用：`pnpm add @feathersjs/socket-commons`、`yarn add @feathersjs/socket-commons`、`bun add @feathersjs/socket-commons`

## README

# @ feathersjs/ socket- commons (英语)

[![Greenkeeper badge](https://badges.greenkeeper.io/feathersjs/socket-commons.svg)](https://greenkeeper.io/)

[![Build Status](https://travis-ci.org/feathersjs/socket-commons.png?branch=master)](https://travis-ci.org/feathersjs/socket-commons) [![Test Coverage](https://codeclimate.com/github/feathersjs/socket-commons/badges/coverage.svg)](https://codeclimate.com/github/feathersjs/socket-commons/coverage) [![Dependency Status](https://img.shields.io/david/feathersjs/socket-commons.svg?style=flat-square)](https://david-dm.org/feathersjs/socket-commons) [![Download Status](https://img.shields.io/npm/dm/@feathersjs/socket-commons.svg?style=flat-square)](https://www.npmjs.com/package/@feathersjs/socket-commons)

> 共享 Websocket 供应商的功能 [@feathers/ socketio (英语)](https://github.com/feathersjs/socketio) 财务报告和审定财务报表 [@ feathersjs/ primus (英语)](https://github.com/feathersjs/primus)。仅打算供其他Feathers实时供应商内部使用.

## 关于

`@feathersjs/socket-commons` 包含Feathers实时供应商的内部共享功能(目前为Socket.io和Primus).

`lib/client.js` 是基本套接字服务客户端 `lib/index.js` 返回可配置的函数,并需要下列选项:

-   `done` - 承诺一旦建立实时协议服务器就能解决
-   `emit` - 向套接字发送数据的方法的名称(`'emit'` 用于Socket.io和 `'send'` 用于Primus)
-   `socketKey` - 存储实际套接字连接的字符串或ES6符号
-   `getParams` - 返回套接字连接选项的函数

## 频道

频道为双向羽毛服务供应商提供频道功能. 例如被Socket.io和Primus提供商用来快速确定要发送到连接客户端的信息.

```
const channels = require('@feathersjs/socket-commons/lib/channels');
```

## 文档

### `app.channel(... names)`

返回一个命名或合并的通道对象.

```js
const channel = app.channel('test'); // return a `test` channel

channel.join(connection); // join a channel
channel.leave(connection); // leave a channel

channel.filter(connection => {}) // return a new channel with filtered connections
channel.length // return the number of connections
channel.connections // all connections in this channel

const combined = app.channel('test', 'other'); // return a combined channel

combined.join(connection); // join the `test` and `other` channel
combined.leave(connection); // leave the `test` and `other` channel

channel.filter(connection => {}) // return a new channel with filtered connections (connections will only be iterated once)
combined.length // return the number of connections
combined.connections // all connections in the combined channel (if a connection is in multiple channels it will only show once)
```

### `app.service('servicename').publish(event, callback)`, (中文(简体) ), `app.service('servicename').publish(callback)`

为返回(名称或合并)频道的服务和事件(或所有事件)注册一个出版回调.

```js
app.use('/test', {
  create(data) {
    return Promise.resolve(data);
  }
});

// `created` event for the `test` service
app.service('test').publish('created', (data, hook) =>
  app.channel('*')
);

// `created` event for the `test` service, sending different data to two different channels
app.service('test').publish('created', (data, hook) => {
  return [
    app.channel('admins'),
    app.channel('users').send(_.omit(data, 'groups', 'email'))
  ];
});

// All events for all services
app.publish((data, hook) =>
  app.channel('*')
);

// All `created` events for all services
app.publish('created', (data, hook) =>
  app.channel('*')
);

// All events for `test` service
app.service('test').publish((data, hook) =>
  app.channel('*')
);
```

### `app.on('publish', function(event, channel, hook) {})`

每次有连接要发布的服务事件都会发送的事件. `channel` 是一个包含所有连接的合并信道来发布此事件.

> _说明:_ 如果没有频道或连接 `publish` 将不发送事件.

```js
app.on('publish', (event, channel, hook) => {
  channel.connections.forEach(connection => {
    // Do something with `connection`
  });
});
```

## 许可证

版权(c)2015年

凭 [麻省理工学院许可证](https://npmcn.com/package/%40feathersjs/socket-commons/LICENSE)。 。 。 .

## 常见问题

### @feathersjs/socket-commons 是什么？

Websocket 提供者共享功能

### 如何安装 @feathersjs/socket-commons？

在项目目录运行 npm install @feathersjs/socket-commons，或使用 pnpm add / yarn add / bun add。

### @feathersjs/socket-commons 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/feathersjs\_\_socket-commons 是否存在。

### @feathersjs/socket-commons 支持 ESM 还是 CommonJS？

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

### @feathersjs/socket-commons 需要什么版本的 Node\.js？

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

### @feathersjs/socket-commons 可以商用吗？

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

### @feathersjs/socket-commons 的下载量有多少？

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

### @feathersjs/socket-commons 还在维护吗？

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

