Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.nyc_output
coverage
.DS_Store
48 changes: 48 additions & 0 deletions examples/buttons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* This demo works best if you have a video app like Hulu open.
* But works ok on the home screen as well.
*/
import xboxSmartGlass from "../src";

// Start the discovery process.
xboxSmartGlass.startDiscovery();

xboxSmartGlass.on("discovery", async (xbox) => {
xboxSmartGlass.stopDiscovery();

await xbox.connect();

// TODO we need an event for letting us know the channel is ready.
// Also need a way to only turn the channel on if the user wants it.
await new Promise((resolve) => setTimeout(resolve, 5000));

// All of the button options can be accessed like this.
// xbox.Buttons.[Button Name]

// Calling quickPress handles both pressing and releasing a button.
// The button will be pressed for approximately 200ms.
// If you have Hulu open with something playing, you'll now have the seeker bar displayed.
xbox.controller.quickPressButton(xbox.Buttons.DPadRight);

await new Promise((resolve) => setTimeout(resolve, 2000));

// Calls to pressButton do not automatically release.
// If you have Hulu open, you'll now be seeking backwards.
xbox.controller.pressButton(xbox.Buttons.DPadLeft);

// Wait 2 seconds
await new Promise((resolve) => setTimeout(resolve, 2000));

// Now release the button.
xbox.controller.releaseButton(xbox.Buttons.DPadLeft);

// Wait 2 seconds
await new Promise((resolve) => setTimeout(resolve, 2000));

// You can supply a timeout to pressButton and then it WILL automatically
// release after the given time in ms.
await xbox.controller.pressButton(xbox.Buttons.DPadRight, 2000);

// Quick press A to start playing again.
xbox.controller.quickPressButton(xbox.Buttons.A);
});
43 changes: 0 additions & 43 deletions examples/connect.js

This file was deleted.

31 changes: 0 additions & 31 deletions examples/connect_and_disconnect.js

This file was deleted.

150 changes: 0 additions & 150 deletions examples/connect_channels.js

This file was deleted.

14 changes: 0 additions & 14 deletions examples/discovery.js

This file was deleted.

18 changes: 18 additions & 0 deletions examples/discovery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import xboxSmartGlass from "../src";

// Start the discovery process.
xboxSmartGlass.startDiscovery();

xboxSmartGlass.on("discovery", (xbox) => {
// Every time a discovery response is received from an Xbox,
// log that Xbox's ip address and liveId.
console.log(
`Discovered xbox with ip address "${xbox.ip}" and liveId "${xbox.liveId}"`
);

// You can always see all discovered Xboxes by calling
// xboxSmartGlass.getXboxConsoles().
console.log(
`Total discovered consoles: ${xboxSmartGlass.getXboxConsoles().length}`
);
});
43 changes: 43 additions & 0 deletions examples/disposeXbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* In some cases, you may wish to dispose of an Xbox instance
* and have it removed from memory. In order to do this, we need to
* shut down its socket and remove all of its listeners.
*
* ! ! ! ! !
* Once you dispose of an Xbox instance it cannot be revived. The behavior of
* the Xbox instance is undefined once it is disposed. Do not hold a reference to it
* after you call dispose.
*/
import xboxSmartGlass from "../src";

// Start listening for Xbox consoles.
xboxSmartGlass.startDiscovery();

// Emits an Xbox console every time a discovery response is received.
xboxSmartGlass.once("discovery", async (xbox) => {
xboxSmartGlass.stopDiscovery();

await xbox.connect();

console.log("Successfully connected to Xbox");

// Demonstrating that xboxSmartGlass currently knows about this Xbox.
console.log(
`xboxSmartGlass has a reference to this xbox: ${xboxSmartGlass
.getXboxConsoles()
.includes(xbox)}`
);

xbox.dispose();

console.log(
"Xbox has been disposed. Logs should indicate a complete shutdown"
);

// Demonstrating that xboxSmartGlass does NOT currently knows about this Xbox.
console.log(
`xboxSmartGlass has a reference to this xbox: ${xboxSmartGlass
.getXboxConsoles()
.includes(xbox)}`
);
});
29 changes: 29 additions & 0 deletions examples/disposeXboxSmartGlass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
* Only call `xboxSmartGlass.dispose()` when you are completely done with it
* and all Xbox instances. It cannot be revived. And since xboxSmartGlass is a singleton
* you cannot make a new one.
*
* This should only be called in order to cleanly shutdown your NodeJS process.
*
* Behavior of xboxSmartGlass and all Xbox instances is undefined after being disposed.
*/
import xboxSmartGlass from "../src";

// Start listening for Xbox consoles, just to demonstrate
// that the process will cleanly exit once disposed.
xboxSmartGlass.startDiscovery();

// Emits an Xbox console every time a discovery response is received.
xboxSmartGlass.on("discovery", async (xbox) => {
console.log("Received Xbox discovery response!");
});

xboxSmartGlass.on("disposed", () => {
console.log("XboxSmartGlass has been disposed!");
console.log("You can now safely exit.");
});

setTimeout(() => {
xboxSmartGlass.dispose();
}, 5000);
Loading