RPC的socket.io使用的合理设计

时间:2013-11-27 22:41:32

标签: node.js websocket socket.io rpc

我正在构建一个Web应用程序,它使用socket.io来触发远程过程调用,将会话特定数据传回客户端。随着我的应用越来越大,越来越多的用户,我想检查一下我的设计是否合理。

接收websocket通信并触发RPC的服务器看起来像这样:

s.on('run', function(input) {
  client.invoke(input.method, input.params, s.id, function(error, res, more) {
    s.emit('output', {
      method: input.method,
      error: error,
      response: res,
      more: more,
      id: s.id
    });
  });
});

但是,这意味着客户端必须首先emit方法调用,然后监听所有方法返回并拔出其正确的返回值:

socket.on('output', function(res) {
  if (res.id === socket.sessionid) {
    if (!res.error) {
      if(res.method === 'myMethod') {
        var myResponse = res.response;
        // Do more stuff with my response
      });
    }
  }
});

随着我添加越来越多的功能,它开始看起来像一个混乱的设计...有没有更好的方法来做到这一点?

将回调附加到每个函数的传统AJAX方法会更好,但我想利用websockets的好处(例如,减少快速通信的开销)。

0 个答案:

没有答案