【发布时间】:2017-01-06 14:56:37
【问题描述】:
我正在使用 Ruby Socket.IO client simple 并尝试使用 Ruby 复制此 JS 代码,但没有成功。 JS 代码按预期工作,但是,Ruby 版本在“auth”发出时不会产生回调输出“身份验证成功”。
虽然 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
【问题讨论】:
标签: javascript ruby callback socket.io