Browse Source

Send return values back through the IPC and WebSocket interfaces

pull/1482/head
Samuel Attard 6 years ago
parent
commit
35aa287c7b
  1. 30
      docs/PlaybackAPI_WebSocket.md
  2. 14
      src/inject/GPMWebView/playback/handler.js
  3. 8
      src/main/features/core/websocketAPI.js

30
docs/PlaybackAPI_WebSocket.md

@ -268,7 +268,10 @@ As soon as you receive that message (and whenever you want to connect to the Web
### All Powerful Controller
You can use **ANY** method from the `gmusic-utils` library --> https://github.com/gmusic-utils/gmusic.js
You can use **ANY** method from the
* `gmusic.js` library --> https://github.com/gmusic-utils/gmusic.js
* `gmusic-ui.js` library --> https://github.com/gmusic-utils/gmusic-ui.js
All you need to do is send a message to the WebSocket with a stringified JSON object in the form.
@ -280,12 +283,25 @@ All you need to do is send a message to the WebSocket with a stringified JSON ob
}
```
#### Additional commands
#### Return Values
GPMDP have also *"extended"* the standard `gmusic.js` library with some extra namespaces.
If the method has a return value you want to fetch you need to pass a `requestID` property to the WebSocket like so.
```js
{
"namespace": "playback",
"method": "getCurrentTime",
"requestID": 1
}
```
| Namespace | Method | Arguments |
|-----------|--------|-----------|
| `playlists` | `play` | One Argument<br />- A `Playlist` object returned from the `playlist` namespace. |
| `playlists` | `playWithTrack` | Two arguments<br />-A `Playlist` object returned from the `playlist` namespace.<br />-A `Track` object from the `tracks` property of the supplied `playlist` |
You will recieve a response in the following form
```js
{
"namespace": "result",
"type": "return", // This will be "error" if the method threw an error
"value": 10000, // This will be the error object if the method threw an error
"requestID": 1
}
```

14
src/inject/GPMWebView/playback/handler.js

@ -82,7 +82,19 @@ window.wait(() => {
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 || []);
let error;
let result;
try {
result = GPM[cmd.namespace][cmd.method].apply(GPM, cmd.args || []);
} catch (err) {
error = err;
}
Emitter.fire(`execute:gmusic:result_${cmd.requestID}`, {
namespace: 'result',
type: error ? 'error' : 'return',
value: error || result,
requestID: cmd.requestID,
});
}
});
});

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

@ -8,6 +8,7 @@ import WebSocket, { Server as WebSocketServer } from 'ws';
let server;
let oldTime = {};
let uniqID = uuid.v4();
let runas = () => {};
if (process.platform === 'win32') {
@ -179,8 +180,15 @@ const enableAPI = () => {
Emitter.sendToGooglePlayMusic('execute:gmusic', {
namespace: command.namespace,
method: command.method,
requestID: command.requestID || uniqID,
args,
});
if (typeof command.requestID !== 'undefined') {
Emitter.once(`execute:gmusic:result_${command.requestID}`, (event, result) => {
ws.json(result);
});
}
uniqID = uuid.v4();
} else {
throw Error('Bad command');
}

Loading…
Cancel
Save