forked from sourcebots/livestream-overlay-old
-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.js
90 lines (71 loc) · 2.3 KB
/
events.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
81
82
83
84
85
86
87
88
89
90
/*
This code listens to the SSE stream from srcomp-stream and
calls functions to act on state updates.
There is a settings.js file with the URI of the stream.
*/
if (!!window.EventSource) {
var eventSource = new EventSource(streamServerURI);
// Register events to the EventSource
eventSource.addEventListener('ping',onPing, false);
eventSource.addEventListener('open', onStreamOpen, false);
eventSource.addEventListener('error', onStreamError, false);
// Register functions to be overrided
eventSource.addEventListener('team', function(e){
if (typeof onTeamUpdate == 'function') {
onTeamUpdate(JSON.parse(e.data));
}
}, false);
eventSource.addEventListener('last-scored-match', function(e){
if (typeof onlastScoredMatchUpdate == 'function') {
onlastScoredMatchUpdate(JSON.parse(e.data));
}
}, false);
eventSource.addEventListener('match', function(e){
if (typeof onMatchUpdate == 'function') {
onMatchUpdate(JSON.parse(e.data));
}
}, false);
eventSource.addEventListener('current-staging-matches', function(e){
if (typeof onStagingUpdate == 'function') {
onStagingUpdate(JSON.parse(e.data));
}
}, false);
eventSource.addEventListener('current-shepherding-matches', function(e){
if (typeof onShepherdingUpdate == 'function') {
onShepherdingUpdate(JSON.parse(e.data));
}
}, false);
eventSource.addEventListener('current-delay', function(e){
if (typeof onDelayUpdate == 'function') {
onDelayUpdate(JSON.parse(e.data));
}
}, false);
eventSource.addEventListener('knockouts', function(e){
if (typeof onKnockoutsUpdate == 'function') {
onKnockoutsUpdate(JSON.parse(e.data));
}
}, false);
eventSource.addEventListener('tiebreaker', function(e){
if (typeof onTiebreakerUpdate == 'function') {
onTiebreakerUpdate(JSON.parse(e.data));
}
}, false);
} else {
console.log("Unable to connect.");
}
// Functions that aren't overridable
function onPing(e){
if (debug) console.log("ping");
}
function onStreamOpen(e){
if (debug) console.log("Opened stream connection");
}
function onStreamError(e){
if (e.readyState == EventSource.CLOSED) {
if (debug) console.log("Stream connection closed");
}
else{
console.log("An error occurred.");
console.log(e);
}
}