mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-04-20 23:12:13 +00:00
new chromecast message protocol
This commit is contained in:
parent
7757ca7e4e
commit
eae061d997
1 changed files with 13 additions and 19 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (C) 2017-2022 Smart code 203358507
|
// Copyright (C) 2017-2022 Smart code 203358507
|
||||||
|
|
||||||
const EventEmitter = require('eventemitter3');
|
const EventEmitter = require('eventemitter3');
|
||||||
|
const hat = require('hat');
|
||||||
|
|
||||||
const MESSAGE_NAMESPACE = 'urn:x-cast:com.stremio';
|
const MESSAGE_NAMESPACE = 'urn:x-cast:com.stremio';
|
||||||
const CHUNK_SIZE = 20000;
|
const CHUNK_SIZE = 20000;
|
||||||
|
|
@ -33,7 +34,7 @@ const initialize = () => {
|
||||||
|
|
||||||
function ChromecastTransport() {
|
function ChromecastTransport() {
|
||||||
const events = new EventEmitter();
|
const events = new EventEmitter();
|
||||||
const chunks = [];
|
const messages = {};
|
||||||
|
|
||||||
initialize()
|
initialize()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
|
@ -59,26 +60,17 @@ function ChromecastTransport() {
|
||||||
|
|
||||||
function onMessage(_, message) {
|
function onMessage(_, message) {
|
||||||
try {
|
try {
|
||||||
const { chunk, last } = JSON.parse(message);
|
const { id, chunk, index, length } = JSON.parse(message);
|
||||||
chunks.push(chunk);
|
messages[id] = messages[id] || [];
|
||||||
if (!last) {
|
messages[id][index] = chunk;
|
||||||
return;
|
if (Object.keys(messages[id]).length === length) {
|
||||||
|
const parsedMessage = JSON.parse(messages[id].join(''));
|
||||||
|
delete messages[id];
|
||||||
|
events.emit('message', parsedMessage);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
chunks.splice(0, chunks.length);
|
|
||||||
events.emit('message-error', error);
|
events.emit('message-error', error);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let parsedMessage;
|
|
||||||
try {
|
|
||||||
parsedMessage = JSON.parse(chunks.splice(0, chunks.length).join(''));
|
|
||||||
} catch (error) {
|
|
||||||
events.emit('message-error', error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
events.emit('message', parsedMessage);
|
|
||||||
}
|
}
|
||||||
function onApplicationStatusChanged(event) {
|
function onApplicationStatusChanged(event) {
|
||||||
events.emit(cast.framework.CastSession.APPLICATION_STATUS_CHANGED, event);
|
events.emit(cast.framework.CastSession.APPLICATION_STATUS_CHANGED, event);
|
||||||
|
|
@ -165,11 +157,13 @@ function ChromecastTransport() {
|
||||||
const chunk = serializedMessage.slice(start, start + CHUNK_SIZE);
|
const chunk = serializedMessage.slice(start, start + CHUNK_SIZE);
|
||||||
chunks.push(chunk);
|
chunks.push(chunk);
|
||||||
}
|
}
|
||||||
|
const id = hat();
|
||||||
return Promise.all(chunks.map((chunk, index) => {
|
return Promise.all(chunks.map((chunk, index) => {
|
||||||
return castSession.sendMessage(MESSAGE_NAMESPACE, {
|
return castSession.sendMessage(MESSAGE_NAMESPACE, {
|
||||||
|
id,
|
||||||
chunk,
|
chunk,
|
||||||
last: index === chunks.length - 1,
|
index,
|
||||||
|
length: chunks.length
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue