Chromecast cannot receive custom messages (CAF Receiver)

2.6k views Asked by At

I am working with react native wrapper for Google Cast SDK and I couldn't send a message from the sender to receiver. I am able to cast media or pause and resume it. The problem is only about custom messages. My custom message listener is never called in the receiver side. Should the message have a specific structure that I am missing? Thanks in advance.

Sender:

  GoogleCast.initChannel('urn:x-cast:testChannel');

  GoogleCast.sendMessage('urn:x-cast:testChannel', 'testMessage');

Receiver:

const context = cast.framework.CastReceiverContext.getInstance();
const CUSTOM_CHANNEL = 'urn:x-cast:testChannel';
context.addCustomMessageListener(CUSTOM_CHANNEL, function(customEvent) {
    // handle customEvent.
    console.log('event received');
});

Edit: I am able to send message from receiver to sender:

Receiver:

context.sendCustomMessage(CUSTOM_CHANNEL , undefined,  'myMessage');

Sender:

GoogleCast.EventEmitter.addListener(GoogleCast.CHANNEL_MESSAGE_RECEIVED, ({undefined, message}) => {
  console.log(message);
}); 
1

There are 1 answers

2
D. Silva On

I think this is problem bellow

https://issuetracker.google.com/issues/117136854#comment7

So try this...

Send

let message = {msg: 'testMessage'};
message = JSON.stringify(message);

GoogleCast.sendMessage('urn:x-cast:testChannel', message);

and Receiver

  const CHANNEL = 'urn:x-cast:testChannel';
  const ctx = cast.framework.CastReceiverContext.getInstance();
  const options = new cast.framework.CastReceiverOptions();
  const objToSender = 
  {
    type: 'status',
    message: 'Playing'
  };

  options.customNamespaces = Object.assign({});
  options.customNamespaces[CHANNEL] = cast.framework.system.MessageType.JSON;

  //receiving sender message
  ctx.addCustomMessageListener(CHANNEL,  customEvent => document.getElementById("main").innerHTML = customEvent.data.msg);

  //message to sender app
  ctx.sendCustomMessage(CHANNEL, objToSender);

  ctx.start(options);