Note: This example app runs on NodeJS, but the receiver framework is just a JS file that will run on any web server. (see Receiver Framework for information about how to use the framework in your own projects)
To run this example application locally
- Install NodeJS (http://nodejs.org/)
- In the root directory of this project, install express:
$ npm install express
- Run the project:
$ node app.js
- Visit http://localhost:9999 in your browser
The receiver component of our cast framework is a single file. (js/framework/CastFramework.js)
You can easily include it in your own HTML page just like you would include any javascript file:
<!-- include Google's chromecast receiver code -->
<script type="text/javascript" src="//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js"></script>
<!-- include jquery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- include the framework -->
<script src="path/to/CastFramework.js"></script>
Then, you need to add the following javascript to your page (or include it in a separate file):
$(CastFramework).ready(function() {
CastFramework.start('urn:x-cast:com.yourpackage.name');
});
At this point, you will have a working chromecast receiver....except it won't actually do anything.
Our framework works using commands. All messages between the devices and the chromecast are json objects that look like this:
{
command: 'doSomething',
content: {} // arbitrary JSON obj
}
Inside the ready function we made earlier, add the following to listen for the 'doSomething' command:
$(CastFramework).on("doSomething", function(event, clientId, content) {
// clientId is the device that sent the message
// content is the arbitrary JSON obj sent in the original message
// your logic goes here
}
Just add one of these event listeners for each command the receiver is listening for, and you're good to go!
Send a message to a particular device:
CastFramework.sendMessage(clientId, "doSomething", content);
// clientId is the device you are sending the message to
// the second argument is the command
// content is the arbitrary JSON obj that you are sending
Or send to all connected devices:
CastFramework.broadcastMessage('doSomething', content);
And that's it! Yes, it is really that simple. Take a look at our sample applications to see it in action.