Browse Source

Implemented the all powerful WebSocketAPI Controller

reviewable/pr190/r8
Samuel Attard 6 years ago
parent
commit
464ac9bd66
  1. 16
      docs/PlaybackAPI_WebSocket.md
  2. 7
      src/inject/GPMInject/playback/handler.js
  3. 22
      src/main/features/core/websocketAPI.js

16
docs/PlaybackAPI_WebSocket.md

@ -84,4 +84,18 @@ This channel is sent on average every 100 - 200 milliseconds. You **will** rece
## Controlling the application
### *Coming Soon*
### All Powerful Controller
You can use **ANY** method from the `gmusic-utils` library --> https://github.com/gmusic-utils/gmusic.js
All you need to do is send a message to the websocket with a stringified JSON object in the form.
```js
{
"namespace": "playback",
"method": "setPlaybackTime",
"arguments": [10000]
}
```
Detailed object requirements
| Attribute |

7
src/inject/GPMInject/playback/handler.js

@ -39,4 +39,11 @@ window.wait(() => {
lastScrobble = currentSong;
}
});
Emitter.on('execute:gmusic', (event, cmd) => {
if (window.GPM && GPM[cmd.namespace] && GPM[cmd.namespace][cmd.method]
&& typeof GPM[cmd.namespace][cmd.method] === 'function') {
GPM[cmd.namespace][cmd.method].apply(GPM, cmd.args || []);
}
});
});

22
src/main/features/core/websocketAPI.js

@ -54,6 +54,28 @@ const enableAPI = () => {
});
};
ws.on('message', (data) => {
try {
const command = JSON.parse(data);
if (command.namespace && command.method) {
const args = command.arguments || [];
if (!Array.isArray(args)) {
throw Error('Bad arguments');
}
Emitter.sendToGooglePlayMusic('execute:gmusic', {
namespace: command.namespace,
method: command.method,
args,
});
} else {
throw Error('Bad command');
}
} catch (err) {
console.log('WebSocketAPI Error: Invalid message recieved'); // eslint-disable-line
console.log(data); // eslint-disable-line
}
});
ws.channel('playState', PlaybackAPI.isPlaying());
if (PlaybackAPI.currentSong()) {
ws.channel('song', PlaybackAPI.currentSong());

Loading…
Cancel
Save