Chromecast无法接收自定义消息(CAF接收器)

时间:2018-07-26 17:54:45

标签: chromecast google-cast google-cast-sdk caf-receiver-sdk

我正在使用Google Cast SDK的react native包装器,但无法将消息从发送者发送到接收者。我可以投放媒体,也可以暂停并继续播放。问题仅与自定义消息有关。在接收方永远不会调用我的自定义消息侦听器。消息是否应该具有我所缺少的特定结构?提前致谢。

发件人:

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

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

收件人:

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');
});

编辑:我能够将消息从接收者发送到发送者:

接收器:

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

发件人:

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

1 个答案:

答案 0 :(得分:0)

我认为这是问题所在

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

所以试试这个...

发送

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

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

和接收器

  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);
相关问题