Browse Source

implement ws handshake

activeAddress
Ivan Socolsky 10 years ago
parent
commit
221431d6cb
  1. 10
      app.js
  2. 6
      lib/eventbroadcaster.js
  3. 6
      lib/server.js
  4. 58
      lib/wsapp.js

10
app.js

@ -1,6 +1,7 @@
#!/usr/bin/env node
var ExpressApp = require('./lib/expressapp');
var WsApp = require('./lib/wsapp');
var basePath = process.env.BWS_BASE_PATH || '/bws/api';
var port = process.env.BWS_PORT || 3001;
@ -11,14 +12,9 @@ var app = ExpressApp.start({
//app.listen(port);
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(port);
var ws = WsApp.start(server);
io.sockets.on('connection', function(socket) {
socket.emit('message', {
'message': 'hello world'
});
});
server.listen(port);
console.log('Bitcore Wallet Service running on port ' + port);

6
lib/eventbroadcaster.js

@ -1,3 +1,5 @@
'use strict';
var log = require('npmlog');
log.debug = log.verbose;
var inherits = require('inherits');
@ -8,8 +10,8 @@ function EventBroadcaster() {};
nodeutil.inherits(EventBroadcaster, events.EventEmitter);
EventBroadcaster.prototype.broadcast = function(service, args) {
this.emit(service, args);
EventBroadcaster.prototype.broadcast = function(eventName, serviceInstance, args) {
this.emit(eventName, serviceInstance, args);
};
var _eventBroadcasterInstance;

6
lib/server.js

@ -166,8 +166,8 @@ WalletService.prototype._verifySignature = function(text, signature, pubKey) {
*
* @param {Object} args
*/
WalletService.prototype._emit = function(args) {
EventBroadcaster.broadcast(this, args);
WalletService.prototype._emit = function(eventName, args) {
EventBroadcaster.broadcast(eventName, this, args);
};
/**
@ -190,7 +190,7 @@ WalletService.prototype._notify = function(type, data) {
ticker: this.notifyTicker++,
});
this.storage.storeNotification(walletId, n, function() {
self._emit(n);
self._emit('notification', n);
});
};

58
lib/wsapp.js

@ -0,0 +1,58 @@
'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.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.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;
}
WalletService.getInstanceWithAuth(data, function(err, res) {
var room = res.walletId;
if (err) {
socket.emit('unauthorized');
socket.disconnect();
return;
}
socket.join(room);
socket.emit('authorized');
});
});
});
};
module.exports = WsApp;
Loading…
Cancel
Save