-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
executable file
·420 lines (360 loc) · 15 KB
/
index.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:og="http://ogp.me/ns#"
xmlns:fb="http://www.facebook.com/2008/fbml"
itemscope itemtype="http://schema.org/WebPage">
<head>
<meta charset="utf-16">
<title>coworking</title>
<meta name="description" content="">
<meta name="keywords" content="">
<meta property="og:type" content="website">
<meta property="og:title" content="">
<meta property="og:description" content="">
<meta property="og:url" content="">
<meta property="og:image" content="">
<meta property="og:site_name" content="">
<meta name="git:tag" content="@@tag">
<meta name="git:rev" content="@@rev">
<meta name="git:date" content="@@date">
<meta name="viewport" content="width=600">
<meta name="apple-mobile-web-app-capable" content="no">
<meta name="mobile-web-app-capable" content="yes">
<link href='http://fonts.googleapis.com/css?family=Lobster' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="app.css">
</head>
<body>
<div id="app">
<section id="users"><video id="myvideo" muted autoplay></video><img id="mypic" /></section>
<div id="chatcontainer"><div><div id="chatbox"></div></div></div>
<div id="controls">
<textarea id="msg" placeholder="Chat message"></textarea>
</div>
</div>
<canvas id="photo" style="display: none;" width="160" height="120"></canvas>
<audio id="alert" src="beepbeep.mp3" style="display: none;"></audio>
<!--<script type="text/javascript" src="//l2.io/ip.js?var=myip"></script>-->
<script type="text/javascript" src="//cdn.temasys.com.sg/skylink/skylinkjs/0.5.x/skylink.complete.min.js"></script>
<script type="text/javascript" src="helpers.js"></script>
<script type="text/javascript">
var skylink = new Skylink(),
$video = document.getElementById('myvideo'),
$pic = document.getElementById('mypic'),
$users = document.getElementById('users'),
$msg = document.getElementById('msg'),
$chatcontainer = document.getElementById('chatcontainer').children[0];
$chatbox = document.getElementById('chatbox'),
$photo = document.getElementById('photo'),
$alert = document.getElementById('alert'),
user = {},
peer_pics = {},
peer_audios = [],
audioTimeouts = {},
last_pic = null,
capture_timeout = null,
click_timeout = null,
online = false,
focussed = true,
mediaOK = null,
muted = false,
hash = location.hash.substr(1).replace('_','-'),
room = location.hash.length > 1 ? location.hash : 'the public hallway';
skylink.setLogLevel(skylink.LOG_LEVEL.DEBUG);
skylink.setDebugMode(true);
skylink.on('incomingStream', function (peerId, stream, isSelf) {
if(isSelf) {
attachMediaStream($video, stream);
}
else {
var $audio = document.createElement('audio');
$audio.id = "audio_" + peerId;
$audio.autoplay = true;
$audio.volume = focussed ? 1 : 0.15;
$users.appendChild($audio);
attachMediaStream($audio, stream);
peer_audios.push($audio);
}
});
skylink.on('incomingMessage', function (message, peerId, peerInfo, isSelf) {
if(message.content.substr(0,1) === '/') {
var msg = message.content.substr(1);
if(msg === 'hail') {
$alert.play();
var $img = document.getElementById("img_" + peerId);
$img.className = "hailing";
setTimeout(function () {
$img.className = "";
}, 5000);
if(!isSelf) {
addMessage('hailed you', 'action', peerId);
}
}
else if(msg.substr(0,8) === 'getaroom') {
var roomPeerId = msg.substr(9);
var name = isSelf ? skylink.getPeerInfo(roomPeerId).userData.name : peerInfo.userData.name;
addMessage('<a href="http://getaroom.io/' + roomPeerId + '" target="_blank">Get an audio/video meeting room with ' + name + '</a>',
'action', isSelf ? roomPeerId : peerId);
}
}
else {
addMessage(message.content, "message", peerId, isSelf);
}
});
skylink.on('dataTransferState', function (state, transferId, peerId, transferInfo, error) {
if(state === skylink.DATA_TRANSFER_STATE.UPLOAD_REQUEST) {
skylink.respondBlobRequest(peerId, true);
console.log("Incoming Request from " + peerId);
}
else if(state === skylink.DATA_TRANSFER_STATE.DOWNLOAD_COMPLETED) {
console.log("Download completed from " + peerId);
var $img = document.getElementById("img_" + peerId);
blob2dataURL(transferInfo.data, function(data) {
peer_pics[peerId] = data;
$img.src = data;
});
}
else if(error && error.message) {
console.log(error.message);
}
});
skylink.on('mediaAccessSuccess', function (stream) {
attachMediaStream($video, stream);
if(!user.name) {
capture_timeout = setTimeout(capture, 2000);
addMessage("Thanks. May I now know your name?", 'bot');
$msg.select();
mediaOK = true;
}
});
skylink.on('mediaAccessError', function (stream) {
addMessage("If you don't want to share your camera and microphone, I'll make you a bot like me. Can I have your name tho?", 'bot');
$msg.select();
mediaOK = false;
});
skylink.on('peerJoined', function (peerId, peerInfo, isSelf) {
if(isSelf) {
online = true;
skylink.setUserData(user);
replay();
if(mediaOK === false) {
$pic.src = last_pic = "http://robohash.org/" + peerId;
$pic.style.display = "inline";
}
addMessage('Welcome to ' + room + ', ' + user.name + '.', 'bot');
}
else {
var info = skylink.getPeerInfo(peerId);
var $img = addUserImage(peerId, info.userData.name);
$img.src = peer_pics[peerId] = "http://robohash.org/" + peerId;
addMessage('is in ' + room, 'action', peerId, false);
}
});
skylink.on('peerLeft', function (peerId, peerInfo, isSelf) {
if(isSelf) {
online = false;
addMessage('Bye', 'bot');
}
else {
var $img = document.getElementById("img_" + peerId);
var $span = document.getElementById("span_" + peerId);
var $audio = document.getElementById("audio_" + peerId);
$users.removeChild($img);
$users.removeChild($span);
$users.removeChild($audio);
addMessage('left ' + room, 'action', peerId, false);
}
});
skylink.init({
//apiKey: '48f72309-6dd1-47bb-9dd2-aa43f4a6a14a',
apiKey: '774b477f-dc20-4374-ba13-3eda09414c28',
defaultRoom: hash
}, function() {
skylink.getUserMedia({
video: {
resolution: {
width: 160,
height: 120
}
},
audio: true
});
addMessage("Hey. welcome to the coworking space. Please share your camera and microphone above so I can see you. <br/><br/> The coworking office is a hangout for remote workers to collaborate. It's an audio live-chat where you can see each other through pictures that update every 5 seconds. Additionally there are a few more things you can do: <ul><li>Your picture will blur to give you a little privacy if you're not actively following this browser tab or your window is inactive</li><li>/mute and /unmute do what you'd expect to your audio and give you more privacy. It'll convert your picture into grayscale to indicate to others you're muted.</li><li>Click on somebodies picture to hail that person</li><li>Type /hail to alert everybody in the office</li><li>Double-click somebodies picture to invite that person into a real-time audio/video meeting room</li><li>Type /clear to empty your local chat history</li><li>Try '/share coffee' or other keywords to post random pictures of Flickr</li><li>The chat supports <a href='https://help.github.com/articles/github-flavored-markdown/' target='_blank'>Github flavored markdown</a></li><li>Join a room by typing /join roomname</li></ul>The coworking office uses WebRTC with <a href='http://temasys.github.io' target='_blank'>Skylink</a> and we don't record pictures or messages. They are send peer-to-peer directly to the other coworkers. This is a public space, so please be respectful to others.<br/><br/><b>You're entering " + room + ".</b>", 'bot');
});
function addUserImage(peerId, name) {
var $img = document.createElement('img');
$img.id = "img_" + peerId;
$users.appendChild($img);
var $span = document.createElement('span');
$span.id = "span_" + peerId;
$span.textContent = name;
$span.onclick = function() {
clearTimeout(click_timeout);
var hail = function() {
skylink.sendMessage('/hail', peerId);
$img.className = "hailing";
setTimeout(function () {
$img.className = "";
}, 5000);
console.log("Hailing " + peerId);
}
click_timeout = setTimeout(hail, 400);
return false
}
$span.ondblclick = function () {
clearTimeout(click_timeout);
skylink.sendMessage('/getaroom ' + peerId, peerId);
console.log("Get a room invite to " + peerId);
return false;
}
$users.appendChild($span);
return $img;
}
function addMessage(message, className, peerId, isSelf) {
var id = (new Date()).getTime();
var $div = document.createElement('div');
$div.className = className + (isSelf ? 'isself': '');
$div.id = 'msg_' + id;
var $img = document.createElement('img');
if(className === 'bot') {
$img.src = 'bot.png';
}
else {
$img.src = isSelf ? last_pic : peer_pics[peerId];
}
if($img.src) {
$div.appendChild($img);
}
var $span_name = document.createElement('span');
$span_name.className = "name";
$span_name.textContent = className === 'bot' ? "Bot" : skylink.getPeerInfo(peerId).userData.name;
$div.appendChild($span_name);
var $span = document.createElement('span');
if(className === 'action') {
$span.innerHTML = message;
}
else {
$span.innerHTML = marked(message);
}
$div.appendChild($span);
if(className !== 'bot') {
localStorage[hash + '_' + id] = $div.outerHTML;
}
$chatbox.appendChild($div);
$chatcontainer.scrollTop = $chatcontainer.scrollHeight;
}
function replay() {
for(var i = 0, len = localStorage.length; i < len; i++) {
var key = localStorage.key(i),
arr = key.split('_');
if(arr.length === 2 && arr[0] === hash && arr[1] > (new Date()).getTime() - 1000 * 60 * 60 * 24 * 5) {
var $div = document.createElement('div');
$div.innerHTML = localStorage[key];
$chatbox.appendChild($div.children[0]);
}
}
$chatcontainer.scrollTop = $chatcontainer.scrollHeight;
}
function capture() {
if(mediaOK !== true) {
return;
}
clearTimeout(capture_timeout);
var ctx = $photo.getContext("2d");
ctx.drawImage($video, 0, 0, 160, 120);
if(!focussed) {
stackBlurCanvasRGB(ctx, 0, 0, 160, 120, 35);
}
if(muted) {
convertToGrayscale(ctx, 0, 0, 160, 120)
}
last_pic = $photo.toDataURL('image/jpeg', 0.8);
$pic.src = last_pic;
$pic.style.display = "inline";
var blob = dataURL2Blob(last_pic);
skylink.sendBlobData(blob, {
size: blob.size,
name: (new Date()).toString()
});
capture_timeout = setTimeout(capture, 5000);
}
/*function adjustVolume($audio, to) {
if(!$audio) {
return;
}
clearTimeout(audioTimeouts[$audio.id]);
$audio.volume = $audio.volume < to ? $audio.volume += 0.05 : $audio.volume -= 0.05;
if(Math.abs($audio.volume - to) > 0.05) {
audioTimeouts[$audio.id] = setTimeout(function() {
adjustVolume($audio, to);
}, 50);
}
}*/
function adjustAllVolume(to) {
for(i in peer_audios) {
peer_audios[i].volume = to;
//adjustVolume(peer_audios[i], to);
}
}
$msg.onkeydown = function(e) {
if(!e.altKey && e.keyCode === 13 && mediaOK !== null) {
if(online) {
if($msg.value === '/clear') {
localStorage.clear();
$chatbox.innerHTML = '';
}
else if($msg.value.substr(0,6) === '/join ') {
window.open(location.origin + location.pathname + '#' + $msg.value.substr(6), $msg.value.substr(6));
}
else if($msg.value === '/mute') {
skylink.muteStream({audioMuted: true});
muted = true;
capture();
}
else if($msg.value === '/unmute') {
skylink.muteStream({audioMuted: false});
muted = false;
capture();
}
else if($msg.value.substr(0,7) === '/share ') {
getPicture($msg.value.substr(7), function(error, url) {
if(error) {
addMessage(error, 'bot');
}
else {
skylink.sendP2PMessage("![powered by Flickr](" + url + ")");
setTimeout(function() {
$chatcontainer.scrollTop = $chatcontainer.scrollHeight;
}, 500);
}
});
}
else {
skylink.sendP2PMessage($msg.value);
}
}
else if(!user.name) {
user.name = $msg.value.replace('\n','').trim();
skylink.joinRoom();
}
$msg.value = '';
$msg.select();
return false;
}
}
$pic.onclick = function() {
capture();
}
window.onfocus = function () {
focussed = true;
capture();
adjustAllVolume(1);
$msg.select();
}
window.onblur = function() {
focussed = false;
adjustAllVolume(0.15);
capture();
}
</script>
</body>
</html>