diff --git a/src/index.jsx b/src/index.jsx index f8b515b19e2e..6078ea745285 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -69,6 +69,8 @@ function init() { window.Events = Events; window.TaskButton = taskButton; + initializeAutoCast(); + serverAddress().then(server => { if (server) { ServerConnections.initApiClient(server); @@ -76,8 +78,6 @@ function init() { }).then(() => { console.debug('initAfterDependencies promises resolved'); - initializeAutoCast(ServerConnections.currentApiClient()); - loadCoreDictionary().then(function () { onGlobalizeInit(); }); diff --git a/src/scripts/autocast.js b/src/scripts/autocast.js index 16f586572390..2c70e42c6f7a 100644 --- a/src/scripts/autocast.js +++ b/src/scripts/autocast.js @@ -1,3 +1,4 @@ +import ServerConnections from 'components/ServerConnections'; import { playbackManager } from '../components/playback/playbackmanager'; import Events from '../utils/events.ts'; @@ -43,16 +44,14 @@ function onOpen() { }); } -export function initialize(apiClient) { - if (apiClient) { - if (apiClient.isWebSocketOpen()) { - console.debug('[autoCast] connection ready'); - onOpen(); - } else { - console.debug('[autoCast] initializing connection listener'); - Events.on(apiClient, 'websocketopen', onOpen); - } - } else { - console.warn('[autoCast] cannot initialize missing apiClient'); - } +export function initialize() { + ServerConnections.getApiClients().forEach(apiClient => { + Events.off(apiClient, 'websocketopen', onOpen); + Events.on(apiClient, 'websocketopen', onOpen); + }); + + Events.on(ServerConnections, 'apiclientcreated', (e, apiClient) => { + Events.off(apiClient, 'websocketopen', onOpen); + Events.on(apiClient, 'websocketopen', onOpen); + }); }