[英]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.