Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use node-castv2 to control url-cast-receiver #44

Open
DaafSamson opened this issue Nov 9, 2017 · 2 comments
Open

How to use node-castv2 to control url-cast-receiver #44

DaafSamson opened this issue Nov 9, 2017 · 2 comments

Comments

@DaafSamson
Copy link

I'm trying to use this lib to control the url-cast-receiver (https://github.com/DeMille/url-cast-receiver).

I manage to connect to my chromecast and start the app, but I can't seem to understand how send a correct message to it to control the URL source. It's docummented in the url-cast-receiver docs

URLs are sent to the receiver by sending messages on the urn:x-cast:com.url.cast namespace.

var msg = { "type": "iframe", "url": "http://example.com" }

But for the life of me I can't seem to get this to work.. Probably because I just don't understand what I'm doing ;)

Any thoughts?

@0187773933
Copy link

const process = require( "process" );
const path = require( "path" );
const global_package_path = process.argv[ 0 ].split( "/bin/node" )[ 0 ] + "/lib/node_modules";
const nodecastor = require( path.join( global_package_path ,  "nodecastor" ) );

( async ()=> {

	// "192.168.1.102"
	// "urn:x-cast:com.url.cast"
	// Display Name = URL Cast Receiver
	// APP_ID = 5CB45E5A

	const device = new nodecastor.CastDevice({
		//friendlyName: 'Attic TV',
		address: "192.168.1.102" ,
		//port: 8009 ,
		//logger: console
	});

	const url_test = "https://www.windy.com/?37.892,-97.305,5";
	function open_url_test() {
		return new Promise( function( resolve , reject ) {
			try {
				device.application( "5CB45E5A" , ( err , application ) => {
					if ( err ) { console.log( err ); return; }
					application.run( "urn:x-cast:com.url.cast" , ( err , session ) => {
						if ( err ) { console.log( error ); return; }
						console.log( session );
						session.send( { "type": "loc" , "url": url_test } , ( err , data ) => {
							console.log( data );
							resolve();
							return;
						});
					});
				});
			}
			catch( error ) { console.log( error ); reject( error ); return; }
		});
	}

	device.on( "connect" , () => {
		device.status( ( err , status ) => {
			if ( err ) { console.log( err ); return; }
			console.log( status );
			open_url_test();
		});
	});

})();

@guy032
Copy link

guy032 commented Oct 9, 2020

I was looking for a solution without mdns (needed to manual install a file from Apple on Windows):

const { Client } = require('castv2');
const ChromecastAPI = require('chromecast-api');
const client = new ChromecastAPI();
client.on('device', (device) => {
  const { host } = device;
  console.log(host);
  const client = new Client();
  client.connect(host, async () => {
    const connection = client.createChannel('sender-0', 'receiver-0', 'urn:x-cast:com.google.cast.tp.connection', 'JSON');
    const receiver   = client.createChannel('sender-0', 'receiver-0', 'urn:x-cast:com.google.cast.receiver', 'JSON');
    connection.send({ type: 'CONNECT' });
    receiver.send({ type: 'LAUNCH', appId: '5CB45E5A', requestId: 1 });
    receiver.on('message', (data) => {
      const { type, status } = data;
      if (type == 'RECEIVER_STATUS') {
        const { applications } = status;
        if (applications) {
          const { transportId, statusText } = applications[0];
          console.log(statusText);
          if (transportId && statusText === 'URL Cast ready...') {
            const castConnection = client.createChannel('sender-0', transportId, 'urn:x-cast:com.google.cast.tp.connection', 'JSON');
            const castReceiver = client.createChannel('sender-0', transportId, 'urn:x-cast:com.url.cast');
            castConnection.send({ type: 'CONNECT' });
            castReceiver.on('message', (data) => console.log(`${transportId}: ${data}`));
            castReceiver.send(JSON.stringify({ "type": "loc", "url": "https://www.windytv.com/" }));
          }
        }
      }
    });
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants