Skip to content

Commit c83dfd2

Browse files
committed
Implement syncplay
1 parent 6db7166 commit c83dfd2

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/apiClient.js

+71
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,19 @@ class ApiClient {
682682
return val.substring(val.indexOf('=') + 1).replace("'", '%27');
683683
}
684684

685+
/**
686+
* Gets the server time as a UTC formatted string.
687+
* @returns {Promise} Promise that it's fulfilled on request completion.
688+
*/
689+
getServerTime() {
690+
const url = this.getUrl('GetUTCTime');
691+
692+
return this.ajax({
693+
type: 'GET',
694+
url: url
695+
});
696+
}
697+
685698
getDownloadSpeed(byteSize) {
686699
const url = this.getUrl('Playback/BitrateTest', {
687700
Size: byteSize
@@ -3242,6 +3255,30 @@ class ApiClient {
32423255
});
32433256
}
32443257

3258+
/**
3259+
* Sends a SyncPlay command.
3260+
* @param {String} sessionId The session that is making the request.
3261+
* @param {String} command The command to request.
3262+
* @param {Object} options Options to send along with the command.
3263+
* @returns {Promise} Promise that it's fulfilled on request completion.
3264+
*/
3265+
sendSyncPlayCommand(sessionId, command, options) {
3266+
if (!sessionId) {
3267+
throw new Error("null sessionId");
3268+
}
3269+
3270+
if (!command) {
3271+
throw new Error("null command");
3272+
}
3273+
3274+
const url = this.getUrl(`SyncPlay/${sessionId}/${command}`, options || {});
3275+
3276+
return this.ajax({
3277+
type: 'POST',
3278+
url: url
3279+
});
3280+
}
3281+
32453282
createPackageReview(review) {
32463283
const url = this.getUrl(`Packages/Reviews/${review.id}`, review);
32473284

@@ -3455,11 +3492,43 @@ function onMessageReceivedInternal(instance, msg) {
34553492
if (user.Id === instance.getCurrentUserId()) {
34563493
instance._currentUser = null;
34573494
}
3495+
} else if (msg.MessageType === 'KeepAlive') {
3496+
console.debug('Received KeepAlive from server.');
3497+
} else if (msg.MessageType === 'ForceKeepAlive') {
3498+
console.debug(`Received ForceKeepAlive from server. Timeout is ${msg.Data} seconds.`);
3499+
instance.sendWebSocketMessage('KeepAlive');
3500+
scheduleKeepAlive(instance, msg.Data);
34583501
}
34593502

34603503
events.trigger(instance, 'message', [msg]);
34613504
}
34623505

3506+
/**
3507+
* Starts a poller that sends KeepAlive messages using a WebSocket connection.
3508+
* @param {Object} instance The WebSocket connection.
3509+
* @param {number} timeout The number of seconds after which the WebSocket is considered lost by the server.
3510+
* @returns {number} The id of the interval.
3511+
*/
3512+
function scheduleKeepAlive(instance, timeout) {
3513+
clearKeepAlive(instance);
3514+
instance.keepAliveInterval = setInterval(() => {
3515+
instance.sendWebSocketMessage('KeepAlive');
3516+
}, timeout * 1000 * 0.5);
3517+
return instance.keepAliveInterval;
3518+
}
3519+
3520+
/**
3521+
* Stops the poller that is sending KeepAlive messages on a WebSocket connection.
3522+
* @param {Object} instance The WebSocket connection.
3523+
*/
3524+
function clearKeepAlive(instance) {
3525+
console.debug('Clearing KeepAlive for', instance);
3526+
if (instance.keepAliveInterval) {
3527+
clearInterval(instance.keepAliveInterval);
3528+
instance.keepAliveInterval = null;
3529+
}
3530+
}
3531+
34633532
function onWebSocketOpen() {
34643533
const instance = this;
34653534
console.log('web socket connection opened');
@@ -3468,13 +3537,15 @@ function onWebSocketOpen() {
34683537

34693538
function onWebSocketError() {
34703539
const instance = this;
3540+
clearKeepAlive(instance);
34713541
events.trigger(instance, 'websocketerror');
34723542
}
34733543

34743544
function setSocketOnClose(apiClient, socket) {
34753545
socket.onclose = () => {
34763546
console.log('web socket closed');
34773547

3548+
clearKeepAlive(socket);
34783549
if (apiClient._webSocket === socket) {
34793550
console.log('nulling out web socket');
34803551
apiClient._webSocket = null;

0 commit comments

Comments
 (0)