-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.js
More file actions
37 lines (29 loc) · 984 Bytes
/
Copy pathClient.js
File metadata and controls
37 lines (29 loc) · 984 Bytes
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
function Client(id) {
this.id = id;
this.counter = ko.observable();
this._otCounter = new OTCounter(this.id, this.counter);
this._peer = new Peer(this.id, {key: "wbs7r9wtos3v7vi"});
this._peer.on("connection", this._otCounter.registerIncomingConnection.bind(this._otCounter));
};
Client.prototype.connect = function(ids) {
var promises = [],
that = this;
_.forEach(ids, function(id) {
var connection = that._peer.connect(id);
var deferred = Q.defer();
promises.push(deferred.promise);
connection.on("open", function() {
// Register connection only once it's ready to use.
deferred.resolve();
that._otCounter.registerOutgoingConnection(connection);
});
});
return promises;
};
Client.prototype.performRandomOperation = function() {
if (Math.floor(Math.random() * 2)) {
this._otCounter.inc();
} else {
this._otCounter.dec();
}
};