-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (38 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
var RtcDataStream = require('rtcstream');
var Promise = require('promise');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var ReplicatorCommon = function(name) {
this.name = name;
this.streams = [];
this.peers = [];
EventEmitter.call(this);
};
util.inherits(ReplicatorCommon, EventEmitter);
module.exports = ReplicatorCommon;
// api
ReplicatorCommon.prototype.receiveData = function(chunk) {
console.log('not implemented');
};
ReplicatorCommon.prototype.replicate = function() {
console.log('not implemented');
};
ReplicatorCommon.prototype.removePeer = function(id) {
var idx = this.peers.indexOf(id);
if (idx >= 0) {
this.peers.splice(idx, 1);
this.streams.splice(idx, 1);
}
};
// common methods
ReplicatorCommon.prototype.addPeer = function(id, dc) {
var self = this;
var stream = new RtcDataStream(this.name + ':' + id, dc);
this.peers.push(id);
this.streams.push(stream);
stream.on('data', this.receiveData.bind(this));
};
ReplicatorCommon.prototype.getPeers = function() {
return this.peers;
};