Store QSS API type in global variable
This commit is contained in:
parent
47b8b484b7
commit
ef3bc125b5
@ -27,15 +27,16 @@ const SIGNALR_CONNECTION_STATE_LABEL = {
|
||||
4: 'Disconnected'
|
||||
};
|
||||
|
||||
let QSS_API_TYPE = null; // will get set to 'framework' or 'core'
|
||||
|
||||
// A unified wrapper to handle both Framework and Core SignalR APIs.
|
||||
const QSS_API_WRAPPER = {
|
||||
connection: null,
|
||||
type: null, // 'framework' or 'core'
|
||||
deviceHubProxy: null, // only populated in 'framework'
|
||||
|
||||
// Map a server-side call
|
||||
invokeServerMethod: function (methodName, ...args) {
|
||||
if (this.type === 'framework') {
|
||||
if (QSS_API_TYPE === 'framework') {
|
||||
|
||||
// Framework API call
|
||||
return this.deviceHubProxy.invoke(methodName, ...args);
|
||||
@ -48,7 +49,7 @@ const QSS_API_WRAPPER = {
|
||||
|
||||
// Map a client-side listener
|
||||
onClientMethod: function (methodName, callback) {
|
||||
if (this.type === 'framework') {
|
||||
if (QSS_API_TYPE === 'framework') {
|
||||
|
||||
// Framework client-side listener
|
||||
this.deviceHubProxy.on(methodName, callback);
|
||||
@ -57,7 +58,7 @@ const QSS_API_WRAPPER = {
|
||||
// Core client-side listener
|
||||
this.connection.on(methodName, callback);
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
function isNullEmptyOrWhitespace(value) {
|
||||
@ -81,6 +82,8 @@ async function startSignalR(callback) {
|
||||
// ASP.NET Framework
|
||||
//
|
||||
|
||||
QSS_API_TYPE = 'framework';
|
||||
|
||||
QSS_API_WRAPPER.connection = $.hubConnection(QUERY_STRING['qssUrl'], { useDefaultPath: false });
|
||||
QSS_API_WRAPPER.deviceHubProxy = QSS_API_WRAPPER.connection.createHubProxy('deviceHub');
|
||||
|
||||
@ -127,7 +130,6 @@ async function startSignalR(callback) {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
QSS_API_WRAPPER.type = 'framework';
|
||||
|
||||
} catch (frameworkErr) {
|
||||
console.warn("Framework connection failed. Trying ASP.NET Core...", frameworkErr);
|
||||
@ -136,6 +138,8 @@ async function startSignalR(callback) {
|
||||
// ASP.NET Core
|
||||
//
|
||||
|
||||
QSS_API_TYPE = 'core';
|
||||
|
||||
QSS_API_WRAPPER.connection = new signalR.HubConnectionBuilder()
|
||||
.withUrl(QUERY_STRING['qssUrl'])
|
||||
.withAutomaticReconnect() // Required to perform automatic reconnects
|
||||
@ -158,10 +162,8 @@ async function startSignalR(callback) {
|
||||
try {
|
||||
await QSS_API_WRAPPER.connection.start();
|
||||
console.log("Connected to ASP.NET Core SignalR!");
|
||||
QSS_API_WRAPPER.type = 'core';
|
||||
} catch (coreErr) {
|
||||
console.error("Both SignalR connection attempts failed.", coreErr);
|
||||
QSS_API_WRAPPER.type = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user