如何动态注册RPC提供程序?

时间:2018-07-31 12:57:59

标签: javascript rpc provider subscriber deepstream.io

我想知道如何在DeepStream中进行动态RPC调用? 例如,当订户呼叫网址http://localhost/myRPCApp/123时,此处的123可以是任何值。那么如何注册提供者方法?

例如:

client.rpc.make(<MY-DYNAMIC-URL>, { patientId: 2 }, (error, result) =>{
    console.log(error, result);
})

在提供者上:

client.rpc.provide(<MY-DYNAMIC-URL>, (data, response) => {
    response.send('Hey there!');    
})

我该如何实现?

1 个答案:

答案 0 :(得分:1)

您可以这样实现动态网址:

1)server.js(已使用:NodeJS)

 GROUP BY 3 

2)cli​​ent.js(已使用:microjs

const ds = deepstream('<URL>');
const randomURL = Math.ceil(Math.random() * 10000).toString();
ds.rpc.provide(randomURL, (data, response) => {
  console.log("received request for: ", randomURL, data);
  response.send(`${Date.now()} Hello from random Service: ${randomURL}`);
})

现在转到

module.exports = async (req, res) => {
  let fullPath = req.url;
  fullPath = fullPath.split('/')[1];
  const result = await ds.rpc.make(fullPath, {}, (err, result) => {
    console.log("response received: ", err, result);
    res.end(result);
  });
  console.log(`result for ${fullPath} is: ${result}`);
}