0

I am trying to implement a currency formatting on text sent from the Chrome Sender app to show on a reciever using the sample Hello World app.

My main confusion here is I don't see anywhere in the code to run the convert function? I am probably overlooking something very basic as Iam a novice at javascript. Any help would be appreciated. https://github.com/googlecast/CastHelloText-chrome/blob/master/chromehellotext.html This is where I am trying to inject the currency formatting. Should I be doing this on the receiver maybe? I don't need currency formatting on the sender necessarily just on the output.

1 Answers1

1

There's two ways to do this:

  1. convert in the sender and send the conversion result to the receiver
  2. send the input to the receiver and convert there

Assuming you have your own convertCurrencty(amount, currency) function, you can do this with the following:

On the sender

There is the update function in the sender that takes the text from the input and sends it to the receiver, so you want to hook into that.

I am talking about line 180 to 182:

function update() {
  var converted = convertCurrency(document.getElementById("input").value, 'USD');
  sendMessage(converted);
}

In the receiver

Alternatively you can do the conversion in the receiver's message callback. I'm referring to lines 80 to 87:

window.messageBus.onMessage = function(event) {
  console.log('Message [' + event.senderId + ']: ' + event.data);
  // display the message from the sender
  displayText(convertCurrency(event.data, 'USD'));
  // inform all senders on the CastMessageBus of the incoming message event
  // sender message listener will be invoked
  window.messageBus.send(event.senderId, event.data);
}

Because basically all the Chromecast code does is send a string from the sender to the receiver and the receiver displays it. So all you need to do is modify the string either before or after it's sent :)

geekonaut
  • 5,714
  • 2
  • 28
  • 30
  • Thank you! I have been playing with this currency format... http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript. Here is a copy of my reciever app if you could lend me a few moments of your time to review that would be great! https://drive.google.com/file/d/0B7IBRkdKpfYSU2ZuRUlmcUNpNjQ/view?usp=sharing – Kjacksonmusic Jan 31 '16 at 16:32
  • Essentially I am having an issue getting this function into my code (function convert(id) { var result = document.getElementById('result'); result.innerText = '$' + document.getElementById(id).value.formatMoney(2); } --- and then using it as you have shown above to convert the text. – Kjacksonmusic Jan 31 '16 at 16:48
  • You've got a few issues in the posted example code. You are using the message string as parameter "id" (?) and search for an element with the id "id" to output the result to, which doesn't exist. So it should be like in the following snippet: https://gist.github.com/AVGP/688c5eeec7af156d2dfa – geekonaut Feb 01 '16 at 14:10
  • Thanks for your time in helping me with this. Can you check my reciever again same drive link as above. After implementing the code above and adding on displayText(convert(event.data)); When I try to cast it just shows "application status is ready" in the chrome extention and the title of the app displays, like nothing is being sent from the sender. If I remove "convert" it works fine. Ideas? – Kjacksonmusic Feb 03 '16 at 01:55
  • Sorry, it actually outputs "undefined" on the receiver. Also, by "works fine" in my above statement i meant, it just sends the numbers without formatting. – Kjacksonmusic Feb 03 '16 at 03:00
  • Right now, there's two things I'd like to say: 1. there's a syntax error, 'cause you're doing "displayTextconvert(..)" instead of "displayText(convert(...))" 2. I'd have to see your sender, but try "displayText(convert(parseFloat(event.data)))" – geekonaut Feb 03 '16 at 07:29
  • again thank you for your time! I have included the parseFloat and formatted it properly (I must not of put it back in properly when I closed it out last night). Now It does not show "undefined" rather just the title. It appears not to be receiving the data at all. Here is a copy of my sender https://drive.google.com/file/d/0B7IBRkdKpfYSTFhleFI4WnpwVlU/view?usp=sharing (just the Google sender app with some added text/image.) – Kjacksonmusic Feb 03 '16 at 23:33
  • again thank you for your time! I have included the parseFloat and formatted it properly (I must not of put it back in properly when I closed it out last night). Now It does not show "undefined" rather just the title. It appears not to be receiving the data at all. Here is a copy of my sender drive.google.com/file/d/0B7IBRkdKpfYSTFhleFI4WnpwVlU/… (just the Google sender app with some added text/image.) – Kjacksonmusic Feb 09 '16 at 13:04