5

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);
}); 
tonder
  • 324
  • 3
  • 15
  • Have you looked at this sample: https://github.com/googlecast/CastHelloText-android – Leon Nicholls Jul 30 '18 at 20:54
  • I am facing the same problem. addCustomMessageListener function is only receiving numbers, not strings. @LeonNicholls ,thanks for your support, but the receiver app of the link you mentioned, is developed in v2, latest is CAF , v3. This problem is specific to CAF. – Anirban iOS Oct 04 '18 at 11:58
  • The issue is being investigated: https://issuetracker.google.com/issues/117136854 – Leon Nicholls Oct 04 '18 at 17:50

1 Answers1

4

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);
D. Silva
  • 71
  • 7