-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcasting.js
98 lines (80 loc) · 2.12 KB
/
casting.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
/* Magic Mirror
* Module: MMM-chromecast
*
* By flo
* MIT Licensed
*
* Using https://github.com/DeMille/url-cast-receiver as chromecast receiver
*/
var applicationID = '5CB45E5A'
, namespace = 'urn:x-cast:com.url.cast'
, receiverDead = false
, session = null;
function initializeCastApi() {
var sessionRequest = new chrome.cast.SessionRequest(applicationID);
var apiConfig = new chrome.cast.ApiConfig(
sessionRequest,
sessionListener,
receiverListener
);
chrome.cast.initialize(
apiConfig,
onSuccess.bind(this, 'initialized ok'),
onErr
);
}
function onErr(err) {
console.log('Err: ' + JSON.stringify(err));
}
function onSuccess(msg) {
console.log('Sucess: ' + msg);
}
function sessionListener(newSession) {
console.log('New session ID:' + newSession.sessionId);
session = newSession;
session.addUpdateListener(sessionUpdateListener);
session.addMessageListener(namespace, receiveMessage);
sendMessage({type:"loc",url: document.location.href});
}
function receiverListener(e) {
(e === 'available')
? console.log('receiver found')
: console.log('no receivers found');
}
function sessionUpdateListener(isAlive) {
if (!isAlive) {
session = null;
}
console.log('Session is alive?: ', isAlive);
}
function receiveMessage(namespace, msg) {
console.log('Receiver said: ' + msg);
}
function sendMessage(msg) {
if (receiverDead || !session) return;
// send msg
session.sendMessage(
namespace,
msg,
function() {
console.log('Message sent: ', msg);
notify('Message sent: ' + JSON.stringify(msg));
},
onErr
);
if (msg.type === 'loc') {
receiverDead = true;
}
}
function notify(msg) {
var el = document.getElementById('notifications'),
notice = document.createElement('div');
if (el == null) return;
notice.className = 'notification';
notice.innerHTML = msg;
el.appendChild(notice);
// notice self destruct timer
setTimeout(function () {
el.removeChild(notice);
}, 5000);
}