-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
159 lines (136 loc) · 4.98 KB
/
client.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
if( window.WebSocket ){
//---------------------------------
// Variables
//---------------------------------
var serviceUrl = "ws://127.0.0.1:3337/streamlabs"
var socket = new WebSocket(API_Socket);
//---------------------------------
// Events
//---------------------------------
socket.onopen = function()
{
// Format your Authentication Information
var auth = {
author: "Surn",
website: "https://www.twitch.tv/surn",
api_key: API_Key,
events: [
"EVENT_GIF","EVENT_MOV", "EVENT_YUT"
]
}
//Send your Data to the server
socket.send(JSON.stringify(auth));
};
socket.onerror = function(error)
{
//Something went terribly wrong... Respond?!
console.log("Error: " + error);
}
socket.onmessage = function (message)
{
var jsonObject = JSON.parse(message.data);
if(jsonObject.event == "EVENT_GIF")
{
//parse jason data
var MySet = JSON.parse(jsonObject.data);
console.log("Parsed" + jsonObject)
//show gif
var image = document.getElementById("myimg");
var badge = document.getElementById("mybadge");
var isgiphy = (MySet.link.indexOf('giphy') > 0 );
image.setAttribute('src', MySet.link);
console.log("got image " + MySet.link);
removeClass(image,'hidden');
if(isgiphy)
{
removeClass(badge,'hidden');
}
setTimeout(function() {
image.removeAttribute('src');
addClass(image,'hidden');
if(isgiphy)
{
addClass(badge,'hidden');
}
}, MySet.duration);
}
if(jsonObject.event == "EVENT_MOV")
{
//parse jason data
var MySet = JSON.parse(jsonObject.data);
console.log("Parsed" + jsonObject)
//show movie
var video = document.getElementById('mymov');
var source = document.createElement('source');
source.setAttribute('src', MySet.link + "#t=" + MySet.start);
console.log("got movie " + MySet.link + "#t=" + MySet.start)
video.appendChild(source);
video.addEventListener('loadedmetadata', function() {
this.currentTime = MySet.start;
}, false);
removeClass(video,'hidden')
video.autoplay = true;
//video.get(0).play();
video.play();
setTimeout(function() {
video.pause();
source.removeAttribute('src'); // empty source
video.load();
video.play();
video.removeChild(source);
addClass(video,'hidden')
}, MySet.duration);
}
if(jsonObject.event == "EVENT_YUT")
{
//parse jason data
var MySet = JSON.parse(jsonObject.data);
console.log("Parsed" + jsonObject)
//show movie
var youtube = document.getElementById('myyut');
youtube.setAttribute('src', "https://www.youtube.com/embed/" + MySet.link + "?controls=0&autoplay=1" + "&start=" + MySet.start +"&end=" + (MySet.start + (MySet.duration / 1000).toString()));
removeClass(youtube,'hidden')
setTimeout(function() {
youtube.removeAttribute('src'); // empty source
addClass(youtube,'hidden')
}, MySet.duration);
}
}
socket.onclose = function ()
{
// Connection has been closed by you or the server
console.log("Connection Closed!");
}
}
function hasClass(el, className)
{
if (el.classList)
return el.classList.contains(className);
return !!el.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'));
}
function addClass(el, className)
{
if (el.classList)
el.classList.add(className)
else if (!hasClass(el, className))
el.className += " " + className;
}
function removeClass(el, className)
{
if (el.classList)
el.classList.remove(className)
else if (hasClass(el, className))
{
var reg = new RegExp('(\\s|^)' + className + '(\\s|$)');
el.className = el.className.replace(reg, ' ');
}
}
/*window.tdiff = []; fred = function(a,b){return a-b;};
window.onload = function(e){
console.log("window.onload", e, Date.now() ,window.tdiff,
(window.tdiff[1] = Date.now()) && window.tdiff.reduce(fred) );
var unmutebutton = document.getElementById('unmuteButton');
unmutebutton.addEventListener('click', function() {
document.getElementById('mymov').muted = false;
});
}*/