I'm using Ruby Socket.IO client simple and trying to replicate this JS code with Ruby with no luck. JS code is working as expected, however, Ruby version does not produce callback output 'Authentication successful' on 'auth' emit.
Although auth in Ruby is successful, since I can emit other private methods after auth. The only question is why callback doesn't work
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!
You pass the method method(:auth_callback)
as a parameter called cb
to auth
method, but you don't do anything with cb
.
cb
is a Method , so you can use call on it.
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)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.