简体   繁体   English

Ruby Socket.IO回调

[英]Ruby Socket.IO callback

I'm using Ruby Socket.IO client simple and trying to replicate this JS code with Ruby with no luck. 我正在简单地使用Ruby Socket.IO客户端,并尝试用Ruby复制此JS代码,但运气不好。 JS code is working as expected, however, Ruby version does not produce callback output 'Authentication successful' on 'auth' emit. JS代码按预期工作,但是,Ruby版本在'auth'发出时不会产生回调输出'Authentication success'。

Although auth in Ruby is successful, since I can emit other private methods after auth. 尽管Ruby中的auth成功,但是由于auth之后我可以发出其他私有方法。 The only question is why callback doesn't work 唯一的问题是为什么回调不起作用

JS JS

var ws = io('https://test.com')

ws.on('connect', function () {
    auth(ws, pubKey, secKey, function (err, auth) {
        if (err) return console.error('Error', err);
        if (auth.success)
            console.log('Authentication successful');
        else
            console.log('Authentication failed');
    });
});

function auth(ws, pubKey, secKey, cb) {
    var data = { apiKey: pubKey, cmd: 'getAuthInfo', nonce: Date.now() };
    var sig = crypto.sign(data, secKey);
    ws.emit('auth', data, sig, cb);
}

Ruby 红宝石

require 'socket.io-client-simple'
require 'date'

ws = SocketIO::Client::Simple.connect 'https://test.com'

socket.on :connect do
   auth(ws, pubKey, secKey, method(:auth_callback))
end

def auth(ws, pubKey, secKey, cb)
   data = { apiKey: pubKey, cmd: 'getAuthInfo', nonce: DateTime.now }
   sig = Crypto.sign(data, secKey)
   ws.emit :auth, [data.to_json, sig, cb]
end

def auth_callback(err, auth)
   if auth.success
      puts 'Authentication successful'
   end
end

auth_callback doesn't get called because you don't call it anywhere! 不会调用auth_callback ,因为您没有在任何地方调用它!

You pass the method method(:auth_callback) as a parameter called cb to auth method, but you don't do anything with cb . 您将方法method(:auth_callback)作为名为cb的参数传递给auth方法,但是您对cb却不做任何事情。

cb is a Method , so you can use call on it. cb是一个Method ,因此您可以在其上使用call

There's not enough data to test your code, so here's a basic example : 没有足够的数据来测试您的代码,所以这是一个基本示例:

cb=3.method(:+)
cb.call(2)
#=> 5

err is neither defined nor used, so you could remove it from def auth_callback(err, auth) . err既未定义也未使用,因此可以将其从def auth_callback(err, auth)删除。

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

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