Primus

npm version Changelog

$ npm install @feathersjs/primus --save

The @feathersjs/primus module allows to call 服务 and receive 事件 via Primus, a universal wrapper for real-time frameworks that supports Engine.IO, WebSockets, Faye, BrowserChannel, SockJS and Socket.IO.

重要

本页介绍如何设置Primus服务器. Primus 客户端 显示了如何在客户端连接到此服务器以及服务调用和实时事件的消息格式.

配置

除了 @feathersjs/primus 之外, 还必须安装您选择的websocket库.

$ npm install ws --save

app.configure(primus(options))

使用给定的 Primus选项 设置Primus传输.

小技巧

一旦服务器启动了 app.listen()app.setup(server), Primus服务器对象就可以作为 app.primus 使用.

const feathers = require('@feathersjs/feathers');
const primus = require('@feathersjs/primus');

const app = feathers();

// Set up Primus with SockJS
app.configure(primus({ transformer: 'ws' }));

app.listen(3030);

app.configure(primus(options, callback))

使用给定的 Primus选项 设置Primus传输, 并使用Primus服务器实例调用回调.

const feathers = require('@feathersjs/feathers');
const primus = require('@feathersjs/primus');

const app = feathers();

// Set up Primus with SockJS
app.configure(primus({
  transformer: 'ws'
}, function(primus) {
  // Do something with primus object
}));

app.listen(3030);

params

Primus请求对象有一个 feathers 属性, 可以在授权期间使用附加服务 params 进行扩展:

app.configure(primus({
  transformer: 'ws'
}, function(primus) {
  // Do something with primus
  primus.use('feathers-referrer', function(req, res){
    // Exposing a request property to services and hooks
    req.feathers.referrer = request.referrer;
  });
}));

app.use('messages', {
  create(data, params, callback) {
    // When called via Primus:
    params.referrer // referrer from request
  }
});

params.provider

对于任何 服务 通过Primus套接字 params.provider 将被设置为 primus.在a 钩子 中, 这可以用来防止外部用户进行服务方法调用:

app.service('users').hooks({
  before: {
    remove(context) {
      // check for if(context.params.provider) to prevent any external call
      if(context.params.provider === 'primus') {
        throw new Error('You can not delete a user via Primus');
      }
    }
  }
});

params.query

params.query 将包含从客户端发送的查询参数.

重要

服务器和客户端之间只传递 params.query, 而 params 的其他部分则没有.这是出于安全原因, 因此客户端无法设置 params.user 或数据库选项.你总是可以从 params.query 映射到 钩子.

params.connection

params.connection 是可以与 事件频道 一起使用的连接对象.它与Primus中间件中的 req.feathers 是相同的对象, 如 params 部分所示 .

Morty Proxy This is a proxified and sanitized view of the page, visit original site.