如何通过 socket.io 进行 RPC? [英] How to do RPC over socket.io?

查看:20
本文介绍了如何通过 socket.io 进行 RPC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个简单的回显服务器(在第一次请求时调整得更长):

var waiting = 8000;io.on('连接', 函数(socket){socket.on('doEcho', 函数(数据){设置超时(函数(){socket.emit('echoDone', 数据);}, 等待);等待 = 1;});});

然后说一个 index.html 客户端脚本:

askEcho ("11111", function (resp) {console.log("回答 11111 " + resp);});askEcho(22222",函数(响应){console.log("22222 的答案" + resp);});

其中 askEcho 是以下错误函数,伪装成 RPC 存根:

function askEcho (text, fun) {socket.emit('doEcho', text);socket.on('echoDone', 函数(数据){乐趣(数据);});}

显然我得到的是以下一团糟

<前>接听 11111 22222回答 22222 22222回答 11111 11111接听 22222 11111

因为我为同一个事件安装了两个监听器.这可以很容易地解决,但我不清楚如何在客户端正确排序响应,在服务器端没有帮助(编程更多).

这一切似乎有点过分的负担.askEcho 函数不能正确且轻松地编码吗?

解决方案

其实有一个简单的技巧:使用计数器来区分答案,并在完成时移除回调(更好地使用 一次 而不是on).这只会在服务器端和客户端造成微小的变化.

让我们展示一下:

回显服务器是(现在没有任何延迟超时):

io.on('connection', function(socket){socket.on('doEcho', function (callCounter, data) {socket.emit('echoDone'+callCounter, 数据);});});

在客户端 (index.html) 中,用户代码仍然是相同的,如所需:

askEcho ("11111", function (resp) {console.log("回答 11111 " + resp);});askEcho(22222",函数(响应){console.log("22222 的答案" + resp);});

这是在与服务器端的团队合作中正常工作的存根 askEcho:

var callCounter = 0;函数 askEcho(文本,有趣){var localCallCounter = ++callCounter;socket.emit('doEcho', localCallCounter, text);socket.once('echoDone'+localCallCounter, 函数(数据){//不需要:socket.removeListener ('echoDone'+localCallCounter);乐趣(数据);});}

Suppose we have a simple echo server (tuned to be longer on the first request):

var waiting = 8000;
io.on('connection', function(socket){   
    socket.on('doEcho', function (data) {
        setTimeout( function () {
            socket.emit('echoDone', data);
        }, waiting);
        waiting = 1;
    });
});

Then say a index.html client script does:

askEcho ("11111", function (resp) {
         console.log("answer for 11111 " + resp);
});
askEcho ("22222", function (resp) {
         console.log("answer for 22222 " + resp);
});

where askEcho is the following buggy function, pretending to be an RPC stub:

function askEcho (text, fun) {
     socket.emit('doEcho', text);
     socket.on('echoDone', function (data) {
         fun ( data );
     });
  }

And obviously what I get is the following mess

answer for 11111  22222
answer for 22222  22222
answer for 11111  11111
answer for 22222  11111

because I installed two listeners for the same event. This can be worked around easily, but I don't see that clear how to get the responses properly ordered at the client, without help (programming more) at the server side.

This all seems a bit too much burden. Can't be the askEcho function coded right and easily?

解决方案

Actually, there is a simple trick: use a counter to differentiate between the answers, and remove the callback when done (even better use once instead of on). This only poses minor changes at server and client sides.

Let's show it:

The echo server is (now without any delaying timeout):

io.on('connection', function(socket){   
    socket.on('doEcho', function (callCounter, data) {    
        socket.emit('echoDone'+callCounter, data);
    });
});

In the client side (index.html) the user code is still the same, as desired:

askEcho ("11111", function (resp) {
    console.log("answer for 11111 " + resp);
});
askEcho ("22222", function (resp) {
    console.log("answer for 22222 " + resp);
});

and this is the stub askEcho that works properly in teamwork with the server side:

var callCounter = 0;
function askEcho (text, fun) {
     var localCallCounter = ++callCounter;
     socket.emit('doEcho', localCallCounter, text);
     socket.once('echoDone'+localCallCounter, function (data) {
         // not required: socket.removeListener ('echoDone'+localCallCounter);
         fun ( data );
     });
 }

这篇关于如何通过 socket.io 进行 RPC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆