简体   繁体   English

如何使用Primus在Feathersjs服务方法上添加更多参数

[英]How to add more parameters on feathersjs service method with primus

I checked this link https://docs.feathersjs.com/real-time/primus.html to setup additional parameters on service method but I didn't figure out how to do it. 我检查了此链接https://docs.feathersjs.com/real-time/primus.html以在服务方法上设置其他参数,但我不知道如何做到这一点。

Below is my service class: 以下是我的服务类别:

create(id, shellId, params) {
     ...

  }

below is the primus configuration: 以下是primus配置:

 app.configure(primus({
    transformer: 'websockets',
    timeout: false
  }, (primus) => {
    primus.library();
    primus.save(path.join(__dirname, '../public/dist/primus.js'));
  }))

In the manual, it mentions something like below: 在手册中,提到了以下内容:

primus.use('todos::create', function(socket, done){
    // Exposing a request property to services and hooks
    socket.request.feathers.referrer = socket.request.referrer;
    done();
  });

but I am not sure what this function is doing. 但我不确定此功能在做什么。 And also I tried to add above code in my application, I will get socket.request is undefined error. 而且我试图在我的应用程序中添加以上代码,我会得到socket.request是未定义的错误。 How to add more parameters on service class in this case? 在这种情况下,如何在服务类上添加更多参数?

You can only use the official service methods and their signatures, in the case of create it is data for the entire data object and params for additional parameters. 您只能使用官方的服务方法和他们的签名,在的情况下createdata对整个数据对象和params额外的参数。 Your service class create looks something like this: 您的服务类create如下所示:

class MyService {
  create(data, params) {
    const { shellId, id } = data;
    // Do something here

    return Promise.resolve({ /* some data here */ });
  }  
}

app.use('/myservice', new MyService());

On the client you can use it through the Feathers client or a direct Primus connection like this: 在客户端上,您可以通过Feathers客户端直接的Primus连接使用它,如下所示:

primus.send('myservice::create', {
  id: 'test',
  shellId: 'testing'
}, (error, data) => {
  console.log('Returned data', data);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM