I have been using custom messages from the receiver to the sender to notify the sender of any status change on the receiver side with great results. Here is an example code snippet to handle various messages from the sender to the receiver -- My app is a Karaoke playlist that queues videos for playing -- it was based on CastVideoPlaylist sample app)
On the sender side, I just register a message listener to update the UI accordingly.
var playlistMessageBus = castReceiverManager.getCastMessageBus(
namespace, cast.receiver.CastMessageBus.MessageType.JSON);
// Create a message handler for the custome namespace channel
playlistMessageBus.onMessage = function(event) {
console.log('Playlist message: ' + JSON.stringify(event));
// Handle the ADD command from the sender
// Other commands could also be supported
switch (event.data.command) {
case 'ADD':
if (playlist == null) {
playlist = [];
}
console.log("Adding " + event.data.playlistItem.contentId + " to playlist");
playlist.push(event.data.playlistItem);
break;
case 'DELETE':
// delete the selected item from playlist
{
var code = 0;
if (playlist == null) {
code = 0;
}
else {
// find the listItem title, then delete it
var index;
for (index = 0; index < playlist.length; index++) {
if (playlist[index].title == event.data.playlistItem.title) {
console.log('Found ' + event.data.playlistItem.title + 'in playlist');
break;
}
}
if (index < playlist.length) {
playlist.splice(index,1);
code = 1;
}
}
var message = {
commandResult: 'CMD_RESULT',
responseCode: code
};
playlistMessageBus.send(event.senderId, JSON.stringify(message));
}
break;
case 'MOVE_TOP':
{
// move the selected listItem to top of the playlist
var code = 0;
if (playlist == null) {
code = 0;
}
else if (playlist.length == 1) {
code = 1;
}
else {
// find the listItem title, then move it to the top
var index;
for (index = 0; index < playlist.length; index++) {
if (playlist[index].title == event.data.playlistItem.title) {
console.log('Found ' + event.data.playlistItem.title + 'in playlist');
break;
}
}
if (index < playlist.length) {
playlist.splice(index,1);
playlist.unshift(event.data.playlistItem);
code = 1;
}
}
var message = {
commandResult: 'CMD_RESULT',
responseCode: code
};
playlistMessageBus.send(event.senderId, JSON.stringify(message));
}
break;
case 'QUERY':
switch (event.data.category) {
case 'PLAY_STATUS':
{
console.log("Sending play status back to sender app");
// inform all senders on the CastMessageBus of the incoming message event
// sender message listener will be invoked
var playState = (inPlay) ? 'PLAYING' : 'STOPPED';
var message = {
playstatus: 'PLAY_STATUS',
state: playState
};
playlistMessageBus.send(event.senderId, JSON.stringify(message));
}
break;
case 'SELECTED_MEDIA':
{
// return the current select media
var listItem = null;
if (curLoadedPlaylistItem != null) {
listItem = {
title: curLoadedPlaylistItem.title,
contentId : curLoadedPlaylistItem.contentID,
image: curLoadedPlaylistItem.image
};
}
var message = {
mediastatus: 'MEDIA_STATUS',
playlistItem: listItem
};
playlistMessageBus.send(event.senderId, JSON.stringify(message));
}
break;
case 'PLAYLIST':
{
// return the playlist
var message = {
playliststatus: 'PLAYLIST',
playlist: playlist
};
playlistMessageBus.send(event.senderId, JSON.stringify(message));
}
break;
default:
console.log("Unrecognized category from sender: " + event.data.category);
break;
}
break;
default:
console.log("Unrecognized command from sender: " + event.data.command);
break;
}
};