-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsender.php
102 lines (86 loc) · 2.54 KB
/
sender.php
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
<?php
require_once("config.php");
?>
<html data-cast-api-enabled="true">
<head>
<title>Hello World Chrome Sender</title>
<link rel="stylesheet" type="text/css" href="/css/sender.css" />
</head>
<body>
<div class="receiver-div">
<h3>Choose a Receiver</h3>
<ul class="receiver-list">
<li>Looking for receivers...</li>
</ul>
</div>
<button class="kill" disabled>Kill the Connection</button><br/><br/>
<input id="msg" type="text" />
<button class="sendMsg">Send Message</button>
</body>
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script>
var cast_api,
cv_activity,
receiverList,
$killSwitch = $('.kill');
window.addEventListener('message', function(event) {
if (event.source === window && event.data &&
event.data.source === 'CastApi' &&
event.data.event === 'Hello') {
initializeApi();
}
});
initializeApi = function() {
if (!cast_api) {
cast_api = new cast.Api();
cast_api.addReceiverListener('<?=$appId?>', onReceiverList);
}
};
onReceiverList = function(list) {
console.log(list);
if (list.length > 0) {
receiverList = list;
$('.receiver-list').empty();
receiverList.forEach(function(receiver) {
$listItem = $('<li><a href="#" data-id="' + receiver.id + '">' + receiver.name + '</a></li>');
$listItem.on('click', receiverClicked);
$('.receiver-list').append($listItem);
});
}
};
receiverClicked = function(e) {
e.preventDefault();
var $target = $(e.target),
receiver = _.find(receiverList, function(receiver) {
return receiver.id === $target.data('id');
});
doLaunch(receiver);
};
doLaunch = function(receiver) {
if (!cv_activity) {
var request = new cast.LaunchRequest('<?=$appId?>', receiver);
$killSwitch.prop('disabled', false);
cast_api.launch(request, onLaunch);
}
};
onLaunch = function(activity) {
if (activity.status === 'running') {
console.log('App Launched!');
cv_activity = activity;
cast_api.sendMessage(cv_activity.activityId, '<?=$namespace?>', {type: 'HelloWorld'});
console.log('message sent');
}
};
$('.sendMsg').on('click', function(){
cast_api.sendMessage(cv_activity.activityId, '<?=$namespace?>', { type: $('#msg').val() });
$('#msg').val('');
});
$killSwitch.on('click', function() {
cast_api.stopActivity(cv_activity.activityId, function(){
cv_activity = null;
$killSwitch.prop('disabled', true);
});
});
</script>
</html>