[英]Ruby Socket.IO callback
我正在简单地使用Ruby Socket.IO客户端,并尝试用Ruby复制此JS代码,但运气不好。 JS代码按预期工作,但是,Ruby版本在'auth'发出时不会产生回调输出'Authentication success'。
尽管Ruby中的auth成功,但是由于auth之后我可以发出其他私有方法。 唯一的问题是为什么回调不起作用
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);
}
红宝石
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.