2017-01-06 41 views
0

我使用Ruby Socket.IO client simple並試圖用Ruby複製這個JS代碼,但沒有運氣。 JS代碼正常工作,但是,Ruby版本在'auth'發出時不會產生回調輸出'Authentication successful'。Ruby Socket.IO回調

儘管在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 

回答

0

auth_callback不會被調用,因爲你沒有任何地方調用它!

您將方法method(:auth_callback)作爲參數cb傳遞給auth方法,但您不會對cb執行任何操作。

cbMethod,所以你可以使用call就可以了。

有沒有足夠的數據來測試你的代碼,所以這裏有一個基本的例子:

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

err既不確定,也不使用,所以你可以從def auth_callback(err, auth)刪除它。

+0

謝謝,我看到你的意思,但它看起來像,我實際上不能得到在ruby中發射方法的響應,就像在js中一樣。看起來像是socket.io-client-simple包裝的限制 –