1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-27 06:23:53 +01:00
Files
fm-dx-webserver/web/js/3las/3las.js
AmateurAudioDude e510ce61e6 Several fixes
* Fixed rare unprompted auto-restart stream bug (bkram) [/web/js/3las/main.js]
* Fixed multiple tooltip bug while RDS PS is tentatively loaded [/web/js/main.js]
* Changed copying of plugin files to symlinks (junction for Windows) [/server/plugins.js]
* Auto-reconnect audio stream on restored/changed internet connection [/web/js/3las/3las.js]
* Main WebSocket connection can be shared with plugins [/web/js/websocket.js] [/web/index.ejs]
2024-07-14 19:52:12 +10:00

179 lines
6.7 KiB
JavaScript

var _3LAS_Settings = /** @class */ (function () {
function _3LAS_Settings() {
this.SocketHost = document.location.hostname ? document.location.hostname : "127.0.0.1";
this.SocketPath = "/";
this.Fallback = new Fallback_Settings();
}
return _3LAS_Settings;
}());
var _3LAS = /** @class */ (function () {
function _3LAS(logger, settings) {
this.Logger = logger;
if (!this.Logger) {
this.Logger = new Logging(null, null);
}
this.Settings = settings;
try {
this.Fallback = new Fallback(this.Logger, this.Settings.Fallback);
this.Fallback.ActivityCallback = this.OnActivity.bind(this);
}
catch (_b) {
this.Fallback = null;
}
if (this.Fallback == null) {
this.Logger.Log('3LAS: Browser does not support either media handling methods.');
throw new Error();
}
if (isAndroid) {
this.WakeLock = new WakeLock(this.Logger);
}
}
Object.defineProperty(_3LAS.prototype, "Volume", {
get: function () {
return this.Fallback.Volume;
},
set: function (value) {
this.Fallback.Volume = value;
},
enumerable: false,
configurable: true
});
_3LAS.prototype.CanChangeVolume = function () {
return true;
};
_3LAS.prototype.Start = function () {
this.ConnectivityFlag = false;
this.Stop(); // Attempt to mitigate the 0.5x speed/multiple stream bug
// This is stupid, but required for Android.... thanks Google :(
if (this.WakeLock)
this.WakeLock.Begin();
try {
if (window.location.protocol === 'https:') {
this.WebSocket = new WebSocketClient(this.Logger, 'wss://' + this.Settings.SocketHost + ':' + location.port.toString() + window.location.pathname + 'audio' , this.OnSocketError.bind(this), this.OnSocketConnect.bind(this), this.OnSocketDataReady.bind(this), this.OnSocketDisconnect.bind(this));
}
else {
this.WebSocket = new WebSocketClient(this.Logger, 'ws://' + this.Settings.SocketHost + ':' + location.port.toString() + window.location.pathname + 'audio' , this.OnSocketError.bind(this), this.OnSocketConnect.bind(this), this.OnSocketDataReady.bind(this), this.OnSocketDisconnect.bind(this));
}
this.Logger.Log("Init of WebSocketClient succeeded");
this.Logger.Log("Trying to connect to server.");
}
catch (e) {
this.Logger.Log("Init of WebSocketClient failed: " + e);
throw new Error();
}
};
_3LAS.prototype.Stop = function () {
try {
// Close WebSocket connection
if (this.WebSocket) {
this.WebSocket.Close();
this.WebSocket.OnClose();
this.WebSocket = null;
this.Logger.Log("WebSocket connection closed.");
}
// Stop WakeLock if it exists and is an Android device
if (isAndroid && this.WakeLock) {
this.WakeLock.End();
this.Logger.Log("WakeLock stopped.");
}
// Reset Fallback if it exists
if (this.Fallback) {
this.Fallback.OnSocketDisconnect();
this.Fallback.Stop();
this.Fallback.Reset();
this.Logger.Log("Fallback reset.");
}
// Reset connectivity flag
if (this.ConnectivityFlag) {
this.ConnectivityFlag = null;
if (this.ConnectivityCallback) {
this.ConnectivityCallback(null);
}
}
this.Logger.Log("3LAS stopped successfully.");
} catch (e) {
this.Logger.Log("Error while stopping 3LAS: " + e);
}
};
_3LAS.prototype.OnActivity = function () {
if (this.ActivityCallback)
this.ActivityCallback();
if (!this.ConnectivityFlag) {
this.ConnectivityFlag = true;
if (this.ConnectivityCallback)
this.ConnectivityCallback(true);
}
};
// Callback function from socket connection
_3LAS.prototype.OnSocketError = function (message) {
this.Logger.Log("Network error: " + message);
this.Fallback.OnSocketError(message);
};
_3LAS.prototype.OnSocketConnect = function () {
this.Logger.Log("Established connection with server.");
this.Fallback.OnSocketConnect();
this.Fallback.Init(this.WebSocket);
};
_3LAS.prototype.OnSocketDisconnect = function () {
this.Logger.Log("Lost connection to server.");
this.Fallback.OnSocketDisconnect();
this.Fallback.Reset();
if (this.ConnectivityFlag) {
this.ConnectivityFlag = false;
if (this.ConnectivityCallback)
this.ConnectivityCallback(false);
}
if (shouldReconnect) {
if (!this.ConnectivityFlag) {
console.log("Initial reconnect attempt...");
this.Stop(); // Attempt to mitigate the 0.5x speed/multiple stream bug
this.Start();
}
// Delay launch of subsequent reconnect attempts by 3 seconds
setTimeout(() => {
let streamReconnecting = false;
let intervalReconnect = setInterval(() => {
if (this.ConnectivityFlag || typeof Stream === 'undefined' || Stream === null) {
console.log("Reconnect attempts aborted.");
clearInterval(intervalReconnect);
} else if (!streamReconnecting) {
streamReconnecting = true;
console.log("Attempting to restart stream...");
this.Stop(); // Attempt to mitigate the 0.5x speed/multiple stream bug
this.Start();
// Wait for reconnect attempt
setTimeout(() => {
streamReconnecting = false;
}, 3000);
}
// Restore user set volume
if (Stream && typeof newVolumeGlobal !== 'undefined' && newVolumeGlobal !== null) {
Stream.Volume = newVolumeGlobal;
console.log(`User volume restored: ${Math.round(newVolumeGlobal * 100)}%`);
}
}, 3000);
}, 3000);
} else {
this.Logger.Log("Reconnection is disabled.");
}
};
_3LAS.prototype.OnSocketDataReady = function (data) {
this.Fallback.OnSocketDataReady(data);
};
return _3LAS;
}());