This article will explain how to send data to an active receiver and how to respond on the receiver.
On our Web SDK, this functionality was implemented from 2.79.0 onwards .
The Google Cast SDK has a global object that everyone can access. This global object is also being used by THEOplayer. The general idea is to retrieve the current active cast session and send a message on a specific namespace.
// Retrieve the current cast session
var castSession = cast.framework.CastContext.getInstance().getCurrentSession();
// Send message on defined namespace channel
castSession.sendMessage('namespace', 'message');
Reference:
// Retrieve the current cast session
CastSession castSession = CastContext.getSharedInstance(context).getSessionManager().getCurrentCastSession();
// Send message on defined namespace channel
castSession.sendMessage('namespace', 'message');
Reference:
On iOS we need a component that implements the GCKGenericChannelDelegate protocol.
// Retrieve the current cast session
let castSession = GCKCastContext.sharedInstance().sessionManager.currenCastSession
// Create the defined namespace channel
let channel = GCKGenericChannel(namespace: "namespace")
// Set the delegate for the namespace channel (GCKGenericChannelDelegate)
channel.delegate = self
// Add the channel to the current cast session
castSession?.add(channel)
// Send message on channel (the error parameter is a pointer at which to store the error result, this may be nil)
channel.sendTextMessage("message", error: nil)
Reference:
The general idea is to retrieve the current active cast session and add an event listener on the specific namespace.
// Retrieve the current cast session
var castSession = cast.framework.CastContext.getInstance().getCurrentSession();
// Add an event listener to the defined namespace channel
castSession.addMessageListener('namespace', (namespace, message) => {
console.log(namespace, message);
})
Reference:
// Retrieve the current cast session
CastSession castSession = CastContext.getSharedInstance(context).getSessionManager().getCurrentCastSession();
// Create callback handler
Cast.MessageReceivedCallback callback = new Cast.MessageReceivedCallback() {
@Override
public void onMessageReceived(CastDevice castDevice, String namespace, String message) {
// Do something with the message
}
};
// Add the message listener to the current cast session
castSession.setMessageReceivedCallbacks('namespace', callback);
Reference:
The delegate of the channel will handle the receiving of messages.
extension ChromecastController : GCKGenericChannelDelegate {
func cast(_ channel: GCKGenericChannel, didReceiveTextMessage message: String, withNamespace protocolNamespace: String) {
print("Channel didReceiveTextMessage", message)
}
}
Reference:
This example is for receiver version 2.
The general idea is to retrieve the receiver manager and add a message listener for the specific namespace. This needs to be done before a THEOplayer instance is created. THEOplayer will call the start method of the receiverManager on initialisation.
// Retrieve the cast receiver manager
var castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
// Retrieve the message bus from the cast receiver manager
var messageBus = castReceiverManager.getCastMessageBus('namespace');
// Add message listener to the message bus
messageBus.onMessage = function(event) {
console.log(event);
}
Reference:
This example is for receiver version 2.
There are multiple possibilities to send data from the receiver:
Broadcast to all connected senders
// 1.
messageBus.onMessage = function(event) {
messageBus.send(event.senderId, 'message');
}
// 2.
messageBus.broadcast('message');
Reference: