You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.0 KiB

'use strict';
var $ = require('preconditions').singleton();
var _ = require('lodash');
var async = require('async');
var log = require('npmlog');
log.debug = log.verbose;
var Uuid = require('uuid');
var WalletService = require('./server');
var Notification = require('./model/notification');
log.level = 'debug';
var io, messageQueue;
var WsApp = function() {};
10 years ago
WsApp._unauthorized = function(socket) {
10 years ago
socket.emit('unauthorized');
socket.disconnect();
};
// WsApp._handleNotification = function(notification) {
// console.log('*** [wsapp.js ln26] notification:', notification); // TODO
// io.to(notification.walletId).emit('notification', notification);
// };
WsApp._initMessageQueue = function(cb) {
function handleNotification(notification) {
io.to(notification.walletId).emit('notification', notification);
};
messageQueue = require('socket.io-client').connect('http://localhost:3380', {
'force new connection': true,
});
messageQueue.on('connect_error', function(err) {
log.warn('Could not connect to message queue server');
});
messageQueue.on('notification', handleNotification);
messageQueue.on('connect', function() {
return cb();
});
};
WsApp.start = function(server, config, cb) {
io = require('socket.io')(server);
async.series([
10 years ago
function(done) {
WsApp._initMessageQueue(done);
},
function(done) {
io.on('connection', function(socket) {
socket.nonce = Uuid.v4();
socket.on('authorize', function(data) {
if (data.message != socket.nonce) return WsApp._unauthorized(socket);
10 years ago
WalletService.getInstanceWithAuth(data, function(err, service) {
if (err) return WsApp._unauthorized(socket);
socket.join(service.walletId);
socket.emit('authorized');
});
});
socket.emit('challenge', socket.nonce);
done();
});
},
], function(err) {
if (cb) return cb(err);
});
};
module.exports = WsApp;