remove redundancy

This commit is contained in:
Ivan Socolsky 2015-03-23 19:07:06 -03:00
parent 221431d6cb
commit 8272232d0c
1 changed files with 11 additions and 17 deletions

View File

@ -18,37 +18,31 @@ var subscriptions = {};
var WsApp = function() {};
WsApp._unauthorized = function() {
socket.emit('unauthorized');
socket.disconnect();
};
WsApp.start = function(server) {
var self = this;
var io = require('socket.io')(server);
WalletService.onNotification(function(serviceInstance, args) {
var walletId = serviceInstance.walletId;
var copayerId = serviceInstance.copayerId;
io.to(walletId).emit('notification', args);
io.to(serviceInstance.walletId).emit('notification', args);
});
io.on('connection', function(socket) {
socket.nonce = Uuid.v4();
socket.emit('challenge', socket.nonce);
socket.on('authorize', function(data) {
if (data.message != socket.nonce) {
socket.emit('unauthorized');
socket.disconnect();
return;
}
if (data.message != socket.nonce) return WsApp.unauthorized();
WalletService.getInstanceWithAuth(data, function(err, res) {
var room = res.walletId;
if (err) {
socket.emit('unauthorized');
socket.disconnect();
return;
}
socket.join(room);
if (err) return WsApp.unauthorized();
socket.join(res.walletId);
socket.emit('authorized');
});
});