-
Notifications
You must be signed in to change notification settings - Fork 276
/
index.html
343 lines (300 loc) · 12.3 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
<!DOCTYPE html>
<head>
<title>WebRTC Simple Calling API + Mobile</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<!-- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -->
<!-- My Phone Number & Dial Areas -->
<!-- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -->
<div class="container">
<div class="row">
<!-- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -->
<!-- MY PHONE NUMBER -->
<!-- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -->
<div class="col-sm-6">
<div id="my-number-section" class="text-center">
<div id="my-number">LOADING</div>
<div id="my-number-title" class="hidden-sm hidden-xs">
this is <strong>your phone number</strong>
</div>
<div id="my-number-permalink">
<span class="glyphicon glyphicon-link"></span>
call link:
<a id="my-number-link" href="..." target="_blank">https://...</a>
</div>
</div>
<div id="pubnub-chat-section" class="text-center">
<input id="pubnub-chat-input" type="text" placeholder="chat here">
<div id="pubnub-chat-output"><div></div></div>
</div>
</div>
<!-- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -->
<!-- Video -->
<!-- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -->
<div class="col-sm-6">
<!-- DISPLAY -->
<div class="row">
<div class="col-xs-12 text-center">
<div id="video-border">
<div class="pubnub-relative">
<div id="video-self"></div>
</div>
<div id="video-display">
<span class="glyphicon glyphicon-flash"></span>
</div>
</div>
<div id="pubnub-relative">
<div id="video-thumbnail"></div>
<div id="pubnub-logo">
<div id="pubnub-logo-img"></div>
</div>
</div>
</div>
</div>
<!-- DIAL CONTROLS -->
<div class="row">
<div class="col-sm-6 col-xs-7">
<div class="form-group"><div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-th"></span>
</div>
<input id="number" class="form-control" type="number" placeholder="Type Recipient's #">
</div></div>
</div>
<div class="col-sm-6 col-xs-5 dial-buttons">
<button id="dial" class="btn btn-primary">
<span class="glyphicon glyphicon-earphone"></span>
</button>
<button id="snap" class="btn btn-success">
<span class="glyphicon glyphicon-camera"></span>
</button>
<button id="hangup" class="btn btn-danger">
<span class="glyphicon glyphicon-ban-circle"></span>
</button>
</div>
</div>
</div>
</div>
</div>
<!-- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -->
<!-- WebRTC Peer Connections -->
<!-- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -->
<!-- IMPORTANT: THIS IS AN UNMINIFIED DEVELOPMENT PUBNUB LIBRARY
replace this with the latest versioned, minified CDN lib link before
you deploy your app to a production enviornment.
https://github.com/pubnub/javascript#cdn-links
-->
<script src="https://cdn.pubnub.com/pubnub.js"></script>
<!-- WebRTC SDK -->
<script src="js/webrtc.js"></script>
<script src="js/sound.js"></script>
<script>(function(){
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Generate Random Number if Needed
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
var urlargs = urlparams();
var my_number = PUBNUB.$('my-number');
var my_link = PUBNUB.$('my-number-link');
var number = urlargs.number || Math.floor(Math.random()*999+1);
my_number.number = number;
my_number.innerHTML = ''+my_number.number;
my_link.href = location.href.split('?')[0] + '?call=' + number;
my_link.innerHTML = my_link.href;
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Update Location if Not Set
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
if (!('number' in urlargs)) {
urlargs.number = my_number.number;
location.href = urlstring(urlargs);
return;
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Get URL Params
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function urlparams() {
var params = {};
if (location.href.indexOf('?') < 0) return params;
PUBNUB.each(
location.href.split('?')[1].split('&'),
function(data) { var d = data.split('='); params[d[0]] = d[1]; }
);
return params;
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Construct URL Param String
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function urlstring(params) {
return location.href.split('?')[0] + '?' + PUBNUB.map(
params, function( key, val) { return key + '=' + val }
).join('&');
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Calling & Answering Service
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
var video_out = PUBNUB.$('video-display');
var img_out = PUBNUB.$('video-thumbnail');
var img_self = PUBNUB.$('video-self');
var phone = window.phone = PHONE({
number : my_number.number // listen on this line
, media : { video: { width:640, height:480 }, audio: true } // <--- Set Camera Resolution
, publish_key : 'pub-c-561a7378-fa06-4c50-a331-5c0056d0163c'
, subscribe_key : 'sub-c-17b7db8a-3915-11e4-9868-02ee2ddab7fe'
, secure : true, ssl: true
});
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Video Session Connected
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function connected(session) {
video_out.innerHTML = '';
video_out.appendChild(session.video);
PUBNUB.$('number').value = ''+session.number;
sounds.play('sound/hi');
console.log("Hi!");
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Video Session Ended
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function ended(session) {
clearInterval(thumbnail.ival);
thumbnail.ival = 0;
set_icon('facetime-video');
img_out.innerHTML = '';
sounds.play('sound/goodbye');
console.log("Bye!");
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Video Session Ended
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function set_icon(icon) {
video_out.innerHTML = '<span class="glyphicon glyphicon-' +
icon + '"></span>';
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Start Phone Call
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function dial(number) {
// Hangup an old call
phone.hangup();
// Dial Number
var session = phone.dial(number);
// No Dupelicate Dialing Allowed
if (!session) return;
// Show Connecting Status
set_icon('send');
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Ready to Send or Receive Calls
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
phone.ready(function(){
// Ready To Call
set_icon('facetime-video');
// Auto Call
if ('call' in urlargs) {
var number = urlargs['call'];
PUBNUB.$('number').value = number;
dial(number);
}
// Make a Phone Call
PUBNUB.bind( 'mousedown,touchstart', PUBNUB.$('dial'), function(){
var number = PUBNUB.$('number').value;
if (!number) return;
dial(number);
} );
// Hanup Call
PUBNUB.bind( 'mousedown,touchstart', PUBNUB.$('hangup'), function() {
phone.hangup();
set_icon('facetime-video');
} );
// Take Picture
PUBNUB.bind( 'mousedown,touchstart', PUBNUB.$('snap'), function() {
var photo = phone.snap();
if (!(photo && photo.image))
return console.error(
'%c Connect your video with a partner first.',
'font-size:30px;background:#f00;color:#fff'
);
img_self.innerHTML = ' ';
img_self.appendChild(photo.image);
setTimeout( function() { img_self.innerHTML = ' ' }, 750 );
} );
});
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Received Call Thumbnail
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function thumbnail(session) {
img_out.innerHTML = '';
img_out.appendChild(session.image);
img_out.appendChild(phone.snap().image);
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Receiver for Calls
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
phone.receive(function(session){
session.message(message);
session.thumbnail(thumbnail);
session.connected(connected);
session.ended(ended);
if (!thumbnail.ival)
thumbnail.ival = setInterval( () => thumbnail(session), 400 );
});
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Chat
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
var chat_in = PUBNUB.$('pubnub-chat-input');
var chat_out = PUBNUB.$('pubnub-chat-output');
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Send Chat MSG and update UI for Sending Messages
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
PUBNUB.bind( 'keydown', chat_in, function(e) {
if ((e.keyCode || e.charCode) !== 13) return true;
if (!chat_in.value.replace( /\s+/g, '' )) return true;
phone.send({ text : chat_in.value });
add_chat( my_number.number + " (Me)", chat_in.value );
chat_in.value = '';
} )
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Update Local GUI
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function add_chat( number, text ) {
if (!text.replace( /\s+/g, '' )) return true;
var newchat = document.createElement('div');
newchat.innerHTML = PUBNUB.supplant(
'<strong>{number}: </strong> {message}', {
message : safetxt(text),
number : safetxt(number)
} );
chat_out.insertBefore( newchat, chat_out.firstChild );
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// WebRTC Message Callback
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function message( session, message ) {
add_chat( session.number, message.text );
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// XSS Prevent
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function safetxt(text) {
return (''+text).replace( /[<>]/g, '' );
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Problem Occured During Init
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
phone.unable(function(details){
console.log("Alert! - Reload Page.");
console.log(details);
set_icon('remove');
});
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Debug Output
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
phone.debug(function(details){
console.log(details);
});
})();</script>
</body>
</html>