Gustavo Maximiliano Cortez
10 years ago
7 changed files with 118 additions and 21 deletions
@ -0,0 +1,25 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var log = require('npmlog'); |
||||
|
log.debug = log.verbose; |
||||
|
var inherits = require('inherits'); |
||||
|
var events = require('events'); |
||||
|
var nodeutil = require('util'); |
||||
|
|
||||
|
function EventBroadcaster() {}; |
||||
|
|
||||
|
nodeutil.inherits(EventBroadcaster, events.EventEmitter); |
||||
|
|
||||
|
EventBroadcaster.prototype.broadcast = function(eventName, serviceInstance, args) { |
||||
|
this.emit(eventName, serviceInstance, args); |
||||
|
}; |
||||
|
|
||||
|
var _eventBroadcasterInstance; |
||||
|
EventBroadcaster.singleton = function() { |
||||
|
if (!_eventBroadcasterInstance) { |
||||
|
_eventBroadcasterInstance = new EventBroadcaster(); |
||||
|
} |
||||
|
return _eventBroadcasterInstance; |
||||
|
}; |
||||
|
|
||||
|
module.exports = EventBroadcaster.singleton(); |
@ -0,0 +1,52 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var $ = require('preconditions').singleton(); |
||||
|
var _ = require('lodash'); |
||||
|
var async = require('async'); |
||||
|
var log = require('npmlog'); |
||||
|
var express = require('express'); |
||||
|
var querystring = require('querystring'); |
||||
|
var bodyParser = require('body-parser') |
||||
|
var Uuid = require('uuid'); |
||||
|
|
||||
|
var WalletService = require('./server'); |
||||
|
|
||||
|
log.debug = log.verbose; |
||||
|
log.level = 'debug'; |
||||
|
|
||||
|
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) { |
||||
|
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) return WsApp.unauthorized(); |
||||
|
|
||||
|
WalletService.getInstanceWithAuth(data, function(err, res) { |
||||
|
if (err) return WsApp.unauthorized(); |
||||
|
|
||||
|
socket.join(res.walletId); |
||||
|
socket.emit('authorized'); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
|
module.exports = WsApp; |
Loading…
Reference in new issue