added PeerJS and initial Network service
This commit is contained in:
parent
7e34253495
commit
76eadc17cf
6 changed files with 188 additions and 12 deletions
|
|
@ -1,8 +1,90 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('cosign.network').factory('Network',
|
||||
function() {
|
||||
angular.module('cosign.network')
|
||||
.factory('NetworkTest', function() {
|
||||
this.f = function() {
|
||||
return 2;
|
||||
};
|
||||
});
|
||||
})
|
||||
.factory('Network', function() {
|
||||
var peer;
|
||||
var connectedPeers = {};
|
||||
|
||||
var _onConnect = function(c, cb) {
|
||||
if (c.label === 'wallet') {
|
||||
var a = peer.connections[c.peer][0];
|
||||
console.log(peer.connections[c.peer]);
|
||||
a.send('------ origin recived -------');
|
||||
|
||||
c.on('data', function(data) {
|
||||
console.log('------ new data ------');
|
||||
console.log(data);
|
||||
|
||||
|
||||
console.log(peer.connections);
|
||||
|
||||
|
||||
c.on('close', function() {
|
||||
alert(c.peer + ' has left the wallet.');
|
||||
delete connectedPeers[c.peer];
|
||||
});
|
||||
});
|
||||
|
||||
setTimeout(function() {
|
||||
a.send('.........................');
|
||||
cb(c.peer);
|
||||
}, 1000);
|
||||
}
|
||||
};
|
||||
|
||||
var _init = function(cb) {
|
||||
peer = new Peer({
|
||||
key: 'lwjd5qra8257b9',
|
||||
debug: 3
|
||||
});
|
||||
|
||||
peer.on('open', cb);
|
||||
};
|
||||
|
||||
var _connect = function(pid, cb) {
|
||||
peer.on('connection', function(conn) {
|
||||
_onConnect(conn, cb);
|
||||
});
|
||||
|
||||
var c = peer.connect(pid, {
|
||||
label: 'wallet',
|
||||
serialization: 'none',
|
||||
reliable: false,
|
||||
metadata: { message: 'hi! cosigners' }
|
||||
});
|
||||
|
||||
c.on('open', function() {
|
||||
c.send('-------oopen-------');
|
||||
});
|
||||
|
||||
c.on('data', function(data) {
|
||||
if (data)
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
c.on('error', function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
};
|
||||
|
||||
var _sendTo = function(pid, data) {
|
||||
if (typeof pids === 'string') {
|
||||
// just send
|
||||
} else if (typeof pids === 'array') {
|
||||
// iter
|
||||
}
|
||||
console.log(data);
|
||||
};
|
||||
|
||||
return {
|
||||
init: _init,
|
||||
connect: _connect,
|
||||
sendTo: _sendTo
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue