-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjsonwebsocketmessenger.js
80 lines (71 loc) · 1.97 KB
/
jsonwebsocketmessenger.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
define(['./utils'],function(utils) {
function JsonWebsocketMessenger(ws) {
this.ws = ws;
if (ws.on) {
ws.on('message',latencySimulator(latency,handleWebsocketMessage.bind(this)));
ws.on('close',handleWebsocketClose.bind(this));
} else {
ws.onmessage = latencySimulator(latency,handleWebsocketMessage.bind(this));
ws.onclose = handleWebsocketClose.bind(this);
}
}
function handleWebsocketMessage(event) {
var data = event.utf8Data || event.data;
// console.log('<',msg.type,data);
var msg = utils.JSONparse(data);
this.onmessage(msg);
}
function handleWebsocketClose() {
this.onclose();
}
(function(p) {
p.send = latencySimulator(latency,function(msg) {
// console.log('>',msg.type,utils.JSONstringify(msg));
this.ws.send(utils.JSONstringify(msg));
});
p.close = function() {
this.ws.close();
};
})(JsonWebsocketMessenger.prototype);
var simulateLatency = false;
var simulateLatencyBase = 100;
var simulateLatencyUnstability = 50;
var simulateLatencySpikeAmount = 100;
var simulateLatencySpikeOccurance = 0.01;
function latency() {
return simulateLatencyBase
+ (Math.random()*simulateLatencyUnstability-simulateLatencyUnstability*0.5)
+ (Math.random() < simulateLatencySpikeOccurance ? simulateLatencySpikeAmount : 0);
}
function latencySimulator(latency,cb) {
var timeout = null;
var queue = [];
function push(/*...*/) {
if (!simulateLatency) {
return cb.apply(this,arguments);
}
queue.push({
trigger: Date.now() + latency(),
self: this,
args: arguments
});
waitForDequeue();
}
function waitForDequeue() {
if (queue.length > 0 && !timeout) {
timeout = setTimeout(dequeue,queue[0].trigger - Date.now());
}
}
function dequeue() {
var now = Date.now();
while (queue.length > 0 && queue[0].trigger <= now) {
var queued = queue.shift();
cb.apply(queued.self,queued.args);
}
timeout = null;
waitForDequeue();
}
return push;
}
return JsonWebsocketMessenger;
});