Sending messages from/to Sender to/from Receiver
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.
Send data to the receiver
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.
Web SDK
// Retrieve the current cast session
var castSession = cast.framework.CastContext.getInstance().getCurrentSession();
// Send message on defined namespace channel
castSession.sendMessage("namespace", "message");
Reference:
- https://developers.google.com/cast/docs/reference/chrome/cast.framework.CastSession
- https://developers.google.com/cast/docs/reference/chrome/cast.framework.CastSession#sendMessage
Android SDK
// Retrieve the current cast session
CastSession castSession = CastContext.getSharedInstance(this).getSessionManager().getCurrentCastSession();
// Send message on defined namespace channel
castSession.sendMessage("namespace", "message");
Reference:
iOS/tvOS SDK and Legacy iOS/tvOS SDK (4.12.x)
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:
- https://developers.google.com/cast/docs/reference/ios/protocol_g_c_k_generic_channel_delegate-p
- https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_session
- https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_generic_channel
- https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_session.html#a3514b6933a579d4255702375f542c16a
- https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_cast_channel.html#ab7f595487ce145f38b2e82e55126789b
Receive data from the receiver
The general idea is to retrieve the current active cast session and add an event listener on the specific namespace.
Web SDK
// 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:
Android SDK
// Retrieve the current cast session
CastSession castSession = CastContext.getSharedInstance(this).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: