0

我想知道如何在DeepStream中进行动态 RPC 调用?例如,当订阅者调用 url 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!');    
})

我怎样才能做到这一点?

4

1 回答 1

0

您可以像这样实现动态 URL:

1)server.js(使用:NodeJS)

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}`);
})

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

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}`);
}

现在去

localhost:port/randomURL
于 2018-08-02T10:55:31.400 回答