Skip to content

Commit 89da107

Browse files
committed
auto-notify on brodacaster start.
1 parent 6f2b852 commit 89da107

File tree

3 files changed

+139
-7
lines changed

3 files changed

+139
-7
lines changed

js/pubnub-stream.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
(async ()=>{
2+
'use strict';
3+
4+
const PubNub = window.PubNub = (setup) => {
5+
for (let key of Object.keys(setup)) PubNub[key] = setup[key];
6+
};
7+
8+
const defaultSubkey = 'demo';
9+
const defaultPubkey = 'demo';
10+
const defaultChannel = 'pubnub';
11+
const defaultOrigin = 'ps.pndsn.com';
12+
13+
const subscribe = PubNub.subscribe = (setup={}) => {
14+
let subkey = setup.subkey || PubNub.subscribeKey || defaultSubkey;
15+
let channel = setup.channel || PubNub.channel || defaultChannel;
16+
let origin = setup.origin || PubNub.origin || defaultOrigin;
17+
let messages = setup.messages || PubNub.messages || (a => a);
18+
let authkey = setup.authkey || PubNub.authKey || '';
19+
let timetoken = setup.timetoken || '0';
20+
let params = `${authkey?'auth='+authkey:''}`;
21+
let decoder = new TextDecoder();
22+
let boundry = /(?<=,"\d{17}"\])[\n,]*/;
23+
let resolver = null;
24+
let promissory = () => new Promise(resolve => resolver = (data) => resolve(data) );
25+
let receiver = promissory();
26+
let reader = null;
27+
let response = null;
28+
let buffer = '';
29+
let subscribed = true;
30+
31+
// Start Stream
32+
startStream();
33+
34+
async function startStream() {
35+
let uri = `https://${origin}/stream/${subkey}/${channel}/0/${timetoken}`;
36+
buffer = '';
37+
38+
try { response = await fetch(`${uri}?${params}`) }
39+
catch(e) { return continueStream(1000) }
40+
41+
try { reader = response.body.getReader() }
42+
catch(e) { return continueStream(1000) }
43+
44+
try { readStream() }
45+
catch(e) { return continueStream(1000) }
46+
}
47+
48+
function continueStream(delay) {
49+
if (!subscribed) return;
50+
setTimeout( () => startStream(), delay || 1000 );
51+
}
52+
53+
async function readStream() {
54+
let chunk = await reader.read().catch(continueStream);
55+
if (!chunk) return;
56+
57+
buffer += decoder.decode(chunk.value || new Uint8Array);
58+
let parts = buffer.split(boundry);
59+
let message = parts[0];
60+
61+
try {
62+
let jsonmsg = JSON.parse(message);
63+
setup.timetoken = timetoken = jsonmsg[1];
64+
65+
jsonmsg[0].forEach(m => {
66+
messages(m);
67+
resolver(m);
68+
receiver = promissory();
69+
});
70+
71+
buffer = parts.slice(1).join('');
72+
}
73+
catch(error) {
74+
// Typically chunk is unfinished JSON in buffer.
75+
// console.error(error);
76+
}
77+
78+
if (!chunk.done) readStream();
79+
else continueStream(10);
80+
}
81+
82+
// Subscription Structure
83+
async function* subscription() {
84+
while (subscribed) yield await receiver;
85+
}
86+
87+
subscription.messages = receiver => messages = setup.messages = receiver;
88+
subscription.unsubscribe = () => subscribed = false;
89+
90+
return subscription;
91+
};
92+
93+
const publish = PubNub.publish = async (setup={}) => {
94+
let pubkey = setup.pubkey || PubNub.publishKey || defaultPubkey;
95+
let subkey = setup.subkey || PubNub.subscribeKey || defaultSubkey;
96+
let channel = setup.channel || PubNub.channel || defaultChannel;
97+
let origin = setup.origin || PubNub.origin || defaultOrigin;
98+
let authkey = setup.authkey || PubNub.authKey || '';
99+
let message = setup.message || 'missing-message';
100+
let uri = `https://${origin}/publish/${pubkey}/${subkey}/0/${channel}/0`;
101+
let params = `${authkey?'auth='+authkey:''}`;
102+
let payload = { method: 'POST', body: JSON.stringify(message) };
103+
104+
try { return await fetch(`${uri}?${params}`, payload) }
105+
catch(e) { return false }
106+
};
107+
108+
})();

tutorials/broadcaster.html

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<!-- Libs and Scripts -->
88
<script src="../js/webrtc-v2.js"></script>
9+
<script src="../js/pubnub-stream.js"></script>
910
<script>(()=>{
1011
'use strict';
1112

@@ -33,7 +34,14 @@
3334
phone.debug( info => console.info(info) );
3435

3536
// As soon as the phone is ready we can make calls
36-
0&& phone.ready(()=>{});
37+
phone.ready(()=>{
38+
PubNub.publish({
39+
channel : "BROADCASTER1234"
40+
, pubkey : pubkey
41+
, subkey : subkey
42+
, message : { notify : "reload" }
43+
});
44+
});
3745

3846
// When Call Comes In
3947
phone.receive(function(session){

tutorials/viewer.html

+22-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<!-- Libs and Scripts -->
88
<script src="../js/webrtc-v2.js"></script>
9+
<script src="../js/pubnub-stream.js"></script>
910
<script>(()=>{
1011
'use strict';
1112

@@ -14,29 +15,39 @@
1415
// The phone *number* can by any string value
1516
const pubkey = 'pub-c-561a7378-fa06-4c50-a331-5c0056d0163c';
1617
const subkey = 'sub-c-17b7db8a-3915-11e4-9868-02ee2ddab7fe';
17-
const number = (''+Math.random()*100000).split('.')[0];
18+
const number = "BROADCAST-VIEWER";//(''+Math.random()*100000).split('.')[0];
1819

1920
// Phone
2021
const phone = PHONE({
2122
number : number
23+
, media : { audio : true, video : true }
2224
, publish_key : pubkey
2325
, subscribe_key : subkey
2426
});
2527

2628
// Local Camera Display
2729
phone.camera.ready( video => {
28-
console.log('Camera Ready');
30+
console.log('Ready');
2931

3032
//phone.$('video-out').appendChild(video);
3133
});
3234

3335
// Debugging Output
3436
phone.debug( info => console.info(info) );
3537

38+
// Receive Broadcaster Updates
39+
PubNub.subscribe({
40+
channel : "BROADCASTER1234"
41+
, pubkey : pubkey
42+
, subkey : subkey
43+
, messages : message => message.notify == "reload" && location.reload(false)
44+
});
45+
3646
// As soon as the phone is ready we can make calls
3747
phone.ready(()=>{
38-
console.log("Calling Broadcaster");
39-
let session = phone.dial("BROADCASTER-TEST");
48+
phone.hangup();
49+
phone.dial("BROADCASTER-TEST");
50+
//setTimeout( () => playing || location.reload, 6000 );
4051
});
4152

4253
// Broadcaster's Video
@@ -50,10 +61,15 @@
5061
playing = true;
5162
console.log("Playing broadcaster's video");
5263
phone.$('video-out').appendChild(broadcaster.video);
53-
setTimeout( () => phone.camera.stop(), 1000 );
64+
setTimeout( phone.camera.stop, 1000 );
65+
setTimeout( phone.toggleAudio, 1000 );
66+
setTimeout( phone.toggleVideo, 1000 );
5467
});
5568

56-
broadcaster.ended( broadcaster => console.log('Stream ended') );
69+
broadcaster.ended( broadcaster => {
70+
playing = false;
71+
console.log('Stream ended');
72+
});
5773
});
5874

5975
})();</script>

0 commit comments

Comments
 (0)