-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.html
347 lines (322 loc) · 11.3 KB
/
view.html
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1">
<!-- Remove Tap Highlight on Windows Phone IE -->
<meta name="msapplication-tap-highlight" content="no"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Viewer</title>
<style>
#mess-messElement {
white-space: preserve;
text-align: center;
}
.remote_image {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: auto;
}
#viewport_div{
position:relative;
border:0;
padding:0;
margin:0 auto;
max-width:100%;
height:100%;
max-height:100%;
}
.viewport_iframes{
position:absolute;
top:0;left:0;
border:0;
padding:0;
margin:0;
max-width:100%;
height:100%;
max-height:100%;
overflow: hidden !important;
pointer-events: none !important;
}
html, body {
text-align:center;
border:0;
margin:0;
padding:0;
width:100%;
height:100%;
max-width:100%;
max-height:100%;
background:black;
}
</style>
</head>
<body>
<div id="viewport_div">
<iframe class="viewport_iframes" sandbox style="visibility: visible;"></iframe>
<iframe class="viewport_iframes" sandbox style="visibility: hidden;"></iframe>
<iframe class="viewport_iframes" sandbox style="visibility: hidden;"></iframe>
<iframe class="viewport_iframes" sandbox style="visibility: hidden;"></iframe>
<iframe class="viewport_iframes" sandbox style="visibility: hidden;"></iframe>
<iframe class="viewport_iframes" sandbox style="visibility: hidden;"></iframe>
</div>
<script src="https://graciano.github.io/mess/src/mess.js"></script>
<script src="https://unpkg.com/[email protected]/dist/peerjs.min.js"></script>
<script type="text/javascript">
(function () {
let div = document.getElementById('viewport_div')
var iframes = div.getElementsByTagName("iframe");
let iframe = null
let iframe_index=iframes.length-1
var lastPeerId = null;
var peer = null; // Own peer object
var peerId = null;
var conn = null;
//https://graciano.github.io/mess/
let mess = new Mess();
mess.init();
let log=function(){
console.log.apply(console.log,arguments)
mess.show(Array.from(arguments).join('\n'));
};
function initialize() {
// Create own peer object with connection to shared PeerJS server
peer = new Peer(null, {
debug: 2
});
peer.on('open', function (id) {
// Workaround for peer.reconnect deleting previous id
if (peer.id === null) {
log('Received null id from peer open');
peer.id = lastPeerId;
} else {
lastPeerId = peer.id;
}
log("ID: " + peer.id, "Awaiting connection...");
let sessionID=null
if(location.hash.match(/^#!/i)){
throw new Error('location hash is a map and needs an api')
}else{
sessionID=location.hash.replace(/^#/, '')
}
let today=new Date();
today.setHours(today.getHours() - 5) //offset the day so people can use it past midnight
today=today.toLocaleString("en-US", {
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
}).replaceAll('/','-').split(',')[0]
join(`6f06212e-614c-4ce0-${today}-${sessionID}`)
});
peer.on('connection', function (c) {
// Allow only a single connection
if (conn && conn.open) {
c.on('open', function() {
c.send("Already connected to another client");
setTimeout(function() { c.close(); }, 500);
});
return;
}
conn = c;
log("Connected to: " + conn.peer);
conn.on('data', function (data) {
log("Data recieved");
var cueString = "<span class=\"cueMsg\">Cue: </span>";
conn.on('data', data => {
log('Received data: ', data);
const img = new Image();
img.src = data.dataURL;
document.body.appendChild(img);
});
// switch (data) {
// case 'Go':
// log(cueString + data);
// break;
// case 'Fade':
// log(cueString + data);
// break;
// case 'Off':
// log(cueString + data);
// break;
// case 'Reset':
// log(cueString + data);
// break;
// default:
// log("<span class=\"peerMsg\">Peer: </span>" + data);
// break;
// };
});
conn.on('close', function () {
log("Connection reset Awaiting connection...");
conn = null;
setTimeout(function(){log('refreshing in 10 seconds'); location.reload()},10000)
});
});
peer.on('disconnected', function () {
log('Connection lost. Please reconnect');
// Workaround for peer.reconnect deleting previous id
peer.id = lastPeerId;
peer._lastServerId = lastPeerId;
peer.reconnect();
});
peer.on('close', function() {
conn = null;
log("Connection destroyed. Please refresh");
setTimeout(function(){log('refreshing in 10 seconds'); location.reload()},10000)
});
peer.on('error', function (err) {
log(err);
});
};
/**
* Create the connection between the two Peers.
*
* Sets up callbacks that handle any events related to the
* connection and data received on it.
*/
function join(id) {
// Close old connection
if (conn) {
conn.close();
}
// Create connection to destination peer specified in the input field
log('Connectng to: ' +id)
conn = peer.connect(id, {
reliable: true
});
conn.on('open', function () {
log("Connected to: " + conn.peer);
// Check URL params for comamnds that should be sent immediately
// var command = getUrlParam("command");
// if (command)
// conn.send(command);
});
// Handle incoming data (messages only since this is the signal sender)
var failsafeTimeout=0
conn.on('data', package => {
clearTimeout(failsafeTimeout)
failsafeTimeout=setTimeout(function(){location.reload();},60000)
console.log('got package',package)
if(package.type=='alive'){
return
}
if(package.type=='frame'){
drawFrame(package)
}
});
conn.on('close', function () {
log("Connection reset Awaiting connection...");
conn = null;
});
};
var oldFrame=''
let drawFrame=function(package){
var iframe_old_index=iframe_index
forceDisplay='html'
displayMedium='iframe' // iframe or root
if(displayMedium=='iframe'){
iframe_index++
iframe=iframes[iframe_index]
if(!iframe){
iframe_old_index = iframes.length-1
iframe_index = 0
iframe=iframes[iframe_index]
}
}
data=package.data||package;
switch(forceDisplay||data.display) {
case 'dataURL':
if(displayMedium == 'iframe'){
throw new Error("need to implement")
}else{ // root
//save images
let images=Array.from(document.getElementsByTagName('img'))
//add image
const img = new Image();
img.className += " remote_image";
img.src = data.dataURL;
document.body.appendChild(img);
//remove other images
for (var i= images.length; i-->0;){
images[i].parentNode.removeChild(images[i]);
}
}
break;
case 'image':
log('Received data: ', data);
break;
case 'html':
let html=data.html
if(oldFrame==html){ //if display medium changes during runtime this will have lingering artifacts
return
}
if(displayMedium == 'iframe'){
iframe.style.width=data.viewport.width+'px'
if("srcdoc" in iframe){
iframe.srcdoc=html
}else{
idoc = iframe.contentWindow || iframe.contentDocument.document || iframe.contentDocument;
idoc.document.open();
idoc.document.write(html);
idoc.document.close();
}
iframe.style.visibility='visible'
oldFrame=html
}else{ // root
let newDoc = document.open("text/html", "replace");
newDoc.write(html);
newDoc.close();
oldFrame=html
}
break;
case 'xhtml': //https://stackoverflow.com/questions/48772331/open-xml-in-new-window-with-javascript
let xhtml=data.xhtml.replace('./kqDraft.js','');
if(oldFrame==xhtml){ //if display medium changes during runtime this will have lingering artifacts
return
}
if(displayMedium == 'iframe'){
let blob = new Blob([xhtml], {type: 'text/xml'});
let url = URL.createObjectURL(blob);
iframe.style.width=data.viewport.width+'px'
//iframe.style.height=data.viewport.height+'px'
iframe.src=url;
//DEVNOTE: TODO dont use datauri write diretctly to the iframe
URL.revokeObjectURL(url); //Releases the resources
oldFrame=url
}else{ // root
throw new Error("need to implement")
}
break;
case 'data':
//this will be custom code in the future
log('Received data: ', data);
break
default:
log('Received data: ', data);
}
if(displayMedium=='iframe'){
setTimeout(function(){
let fram=iframes[iframe_old_index]
if(fram){
fram.style.visibility='hidden'
if("srcdoc" in fram){
fram.srcdoc=''
}else{
let idoc = fram.contentWindow || fram.contentDocument.document || fram.contentDocument;
idoc.document.open();
idoc.document.write('');
idoc.document.close();
}
}
},500) //TODO this should be a callback from when the iframe has rendered
}
}
if(!location.hash){
while(!location.hash){
location.hash=prompt('What session should I connect to?') ||''
}
}
initialize();
})();
</script>
</body>
</html>