Created
August 14, 2018 10:21
-
-
Save ericbrunner/a7810740d916c9191b6b25b0182efb38 to your computer and use it in GitHub Desktop.
SignalR - Javascript Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var signalrutils = { | |
bindConnectionMessage: function() { | |
//debugger;; | |
var messageCallback = function(senderName, message) { | |
//debugger;; | |
if (!message) return; | |
// Log RAW Message | |
if (senderName === undefined || senderName === null) { | |
console.log("[MYINFO] Name : " + senderName); | |
} else { | |
console.log("[MYINFO] Name length= " + senderName.length); | |
console.log("[MYINFO] Name : " + senderName); | |
} | |
console.log("[MYINFO] Message length= " + message.length); | |
debugger; | |
const jsonMessage = JSON.parse(message); | |
const auftragStatusId = jsonMessage.AuftragStatusId; | |
const auftragStatusChanged = jsonMessage.AuftragStatusChanged; | |
const taskName = jsonMessage.TaskName; | |
const workflowTaskId = jsonMessage.WorkflowTaskId; | |
const workflowTaskStatusId = jsonMessage.WorkflowTaskStatusId; | |
const truckDriver = jsonMessage.TruckDriver; | |
const truckAuftrag = jsonMessage.TruckAuftrag; | |
const truckAuftragWorkflow = jsonMessage.TruckAuftragWorkflow; | |
let coreMessage = ""; | |
let stringifiedAspUser = $("#aspUser").val(); | |
let aspUser = JSON.parse(stringifiedAspUser); | |
let lieferantId = parseInt(aspUser.LieferantId); | |
let auftraggeberId = parseInt(aspUser.AuftraggeberId); | |
// ---- Display TA Status Change --- | |
if (auftragStatusChanged) { | |
switch (auftragStatusId) { | |
case 1025: // New Order | |
break; | |
case 1040: // Reject TF => SUB | |
// Mode SUB => Fahrer [UC10] | |
if (truckAuftrag.LieferantId === lieferantId) { | |
coreMessage = "Auftrag " + truckAuftrag.TruckAppId + " wurde vom Fahrer " + truckDriver.FahrerBez + " abgelehnt."; | |
} | |
break; | |
case 1045: // Reject TF => AG | |
// Mode AG => Fahrer [UC11] | |
if (truckAuftrag.FlagDirectOrder && (truckAuftrag.AuftraggeberId === auftraggeberId)) { | |
coreMessage = "Auftrag " + truckAuftrag.TruckAppId + " wurde vom Fahrer " + truckDriver.FahrerBez + " abgelehnt."; | |
} | |
break; | |
case 1050: // Accept Order | |
// Mode SUB => Fahrer [UC10] || Mode AG => Fahrer [UC11] | |
if (truckAuftrag.LieferantId === lieferantId || | |
(truckAuftrag.FlagDirectOrder && (truckAuftrag.AuftraggeberId === auftraggeberId))) | |
{ | |
coreMessage = "Auftrag " + truckAuftrag.TruckAppId + " wurde vom Fahrer " + truckDriver.FahrerBez + " angenommen."; | |
} | |
break; | |
case 2105: // Lkw-Start | |
coreMessage = "Auftrag " + truckAuftrag.TruckAppId + " in Status " + "\"Lkw-Start\""; | |
break; | |
case 2110: // Lkw zu Ladestelle | |
coreMessage = "Auftrag " + truckAuftrag.TruckAppId + " in Status " + "\"Lkw zu Ladestelle\""; | |
break; | |
case 2125: // Lkw bei Ladestelle | |
coreMessage = "Auftrag " + truckAuftrag.TruckAppId + " in Status " + "\"Lkw bei Ladestelle\""; | |
break; | |
case 2140: // Lkw von Ladestelle | |
coreMessage = "Auftrag " + truckAuftrag.TruckAppId + " in Status " + "\"Lkw von Ladestelle\""; | |
break; | |
case 2145: // Lkw Abgabe Ctr | |
coreMessage = "Auftrag " + truckAuftrag.TruckAppId + " in Status " + "\"Lkw Abgabe Ctr\""; | |
break; | |
case 2115: | |
break; | |
case 2150: | |
break; | |
case 2550: | |
break; | |
case 3000: | |
break; | |
case 7000: | |
break; | |
case 8900: | |
break; | |
case 9000: // Cancellation of Order | |
break; | |
default: | |
break; | |
} | |
} | |
// --- Display TAW Status Change --- | |
if (workflowTaskId !== -1 && workflowTaskStatusId !== -1) { | |
// Use-Case [UC10] SUB => Fahrer. Mode SUB (Display Ta Accept, TA Reject) | |
if (truckAuftrag.LieferantId === lieferantId) { | |
let tawStatusText = "Fahrer " + | |
truckDriver.FahrerBez + | |
" hat Task \"" + | |
taskName + | |
"\" soeben in Status " + | |
workflowTaskStatusId + | |
" quittiert."; | |
if (coreMessage === "") { | |
coreMessage = tawStatusText; | |
} else { | |
coreMessage += "\r\n" + tawStatusText; | |
} | |
} else { | |
// Use-Case [UC11] AG => Fahrer (Direct Order) | |
if (truckAuftrag.FlagDirectOrder && truckAuftrag.AuftraggeberId === auftraggeberId) { | |
let tawStatusText = "Fahrer " + | |
truckDriver.FahrerBez + | |
" hat Task \"" + | |
taskName + | |
"\" soeben in Status " + | |
workflowTaskStatusId + | |
" quittiert."; | |
if (coreMessage === "") { | |
coreMessage = tawStatusText; | |
} else { | |
coreMessage += "\r\n" + tawStatusText; | |
} | |
} else { | |
// Mode AG - only get TAW State Changes without Driver Details. | |
if (truckAuftrag.AuftraggeberId === auftraggeberId) { | |
let tawStatusText = " -- Task \"" + | |
taskName + | |
"\" wurde soeben in Status " + | |
workflowTaskStatusId + | |
" quittiert."; | |
if (coreMessage === "") { | |
coreMessage = tawStatusText; | |
} else { | |
coreMessage += "\r\n" + tawStatusText; | |
} | |
} | |
// Mode EG - only get TAW State Changes without Driver Details. | |
if (truckAuftrag.EigentuemerId === auftraggeberId) { | |
// TODO get AG name to display "Auftragnehmer ...." | |
let tawStatusText = " -- Task \"" + | |
taskName + | |
"\" wurde soeben in Status " + | |
workflowTaskStatusId + | |
" quittiert."; | |
if (coreMessage === "") { | |
coreMessage = tawStatusText; | |
} else { | |
coreMessage += "\r\n" + tawStatusText; | |
} | |
} | |
} | |
} | |
} | |
let popUpNotification = $("#appPopupNotification").data("kendoNotification"); | |
popUpNotification.setOptions({ | |
autoHideAfter: 0 //disable auto-hide | |
}); | |
popUpNotification.info(coreMessage); | |
popUpNotification.setOptions({ | |
autoHideAfter: 5000 //disable auto-hide | |
}); | |
// Everyone gets a Grid Refresh if displayed. | |
appLayout.updateDispoGridForSignalrPush(); | |
}; | |
debugger; | |
// Create a function that the hub can call to broadcast messages. | |
appLayout.connection.on('broadcastMessage', messageCallback); | |
appLayout.connection.onclose(appLayout.onConnectionError); | |
}, | |
onConnectionError: function(error) { | |
//debugger;; | |
if (error) { | |
console.error(error.toString()); | |
} | |
if (appLayout.stopSignalrRetry) { | |
console.log("SignalR: stopped retry attempts."); | |
return; | |
} | |
var popUpNotification = $("#appPopupNotification").data("kendoNotification"); | |
popUpNotification.error("SignalR Kommunikation ist unterbrochen. Reconnect in ein paar Sekunden ..."); | |
appLayout.tryReconnect(); | |
}, | |
tryReconnect: function() { | |
setTimeout(function() { | |
//debugger; | |
if (appLayout.stopSignalrRetry) { | |
console.log("SignalR: stopped retry attempts."); | |
return; | |
} | |
appLayout.connection.start() | |
.then(function() { | |
appLayout.onSignalrConnected(); | |
}) | |
.catch(function(error) { | |
appLayout.onSignalrConnectFailed(error); | |
}); | |
}, | |
5000); | |
}, | |
onSignalrConnected: async function() { | |
//debugger;; | |
var stringifiedAspUser = $("#aspUser").val(); | |
var aspUser = JSON.parse(stringifiedAspUser); | |
var name = aspUser.LieferantenName; | |
var auftraggeberId; | |
var lieferantId; | |
let connectionId; | |
debugger; | |
if (appLayout.getMode() === "AGSUB") { | |
try { | |
// Join as AG (targeted Push as receiverId:auftraggeberId) | |
auftraggeberId = parseInt(aspUser.AuftraggeberId); | |
await appLayout.connection.invoke("join", auftraggeberId, name); | |
console.info("SignalR: User " + name + " with AG Id: " + auftraggeberId + " joined."); | |
connectionId = await appLayout.connection.invoke("getConnectionId"); | |
console.info("SignalR: ConnectionId: " + connectionId); | |
} catch (e) { | |
console.error(e.toString()); | |
} | |
try { | |
// Join as SUB (targeted Push as receiverId:lieferantId) | |
lieferantId = parseInt(aspUser.LieferantId); | |
await appLayout.connection.invoke("join", lieferantId, name); | |
console.info("SignalR: User " + name + " with SUB Id: " + lieferantId + " joined.") | |
connectionId = await appLayout.connection.invoke("getConnectionId"); | |
console.info("SignalR: ConnectionId: " + connectionId); | |
} catch (ex) { | |
console.error(ex.toString()); | |
} | |
} else if (appLayout.getMode() === "AG") { | |
auftraggeberId = parseInt(aspUser.AuftraggeberId); | |
appLayout.connection.invoke("join", auftraggeberId, name) | |
.done(() => console.info("SignalR: User " + name + " with AG Id: " + auftraggeberId + " joined.")) | |
.catch(err => console.error()(err.toString())); | |
} else if (appLayout.getMode() === "SUB") { | |
lieferantId = parseInt(aspUser.LieferantId); | |
appLayout.connection.invoke("join", lieferantId, name) | |
.done(() => console.info("SignalR: User " + name + " with SUB Id: " + lieferantId + " joined.")) | |
.catch(err => console.error()(err.toString())); | |
} | |
var popUpNotification = $("#appPopupNotification").data("kendoNotification"); | |
popUpNotification.success("SignalR Kommunikation ist hergestellt."); | |
}, | |
onSignalrConnectFailed: function(error) { | |
//debugger;; | |
if (error) { | |
console.error(error.toString()); | |
} | |
if (appLayout.stopSignalrRetry) { | |
console.log("SignalR: stopped retry attempts."); | |
return; | |
} | |
var popUpNotification = $("#appPopupNotification").data("kendoNotification"); | |
popUpNotification.error("SignalR Verbindungsaufbau fehlgeschlagen. Reconnect in ein paar Sekunden ..."); | |
appLayout.tryReconnect(); | |
}, | |
stopSignalR: function() { | |
var popUpNotification = $("#appPopupNotification").data("kendoNotification"); | |
popUpNotification.success("SignalR Kommunication wird gestopped ..."); | |
appLayout.stopSignalrRetry = true; | |
setTimeout(function() { | |
appLayout.connection.stop() | |
.then(function() { | |
console.log("SignalR Kommunication stopped"); | |
popUpNotification.success("SignalR Kommunication gestopped."); | |
setTimeout(function() { | |
appLayout.stopSignalrRetry = false; | |
}, | |
10000); | |
}) | |
.catch(function(error) { | |
if (error) { | |
console.error(error.toString()); | |
popUpNotification.error("SignalR Stop Fehlermeldung: " + error.toString()); | |
} else { | |
console.error("SignalR Kommunikation konnte nicht gestoppt werden."); | |
} | |
}); | |
}, | |
10000); | |
}, | |
bootStrapSignalR: function() { | |
//debugger;; | |
var currentSignalrHub; | |
if (window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1") { | |
currentSignalrHub = "https://localhost:5001/truckerwebapp"; | |
} else { | |
currentSignalrHub = "https://truckerwebapp-api-dev.azurewebsites.net/truckerwebapp"; | |
} | |
debugger; | |
if (appLayout.connection == null) { | |
appLayout.connection = new signalR.HubConnectionBuilder() | |
.withUrl(currentSignalrHub) | |
.configureLogging(signalR.LogLevel.Trace) | |
.build(); | |
appLayout.connection.serverTimeoutInMilliseconds = 1000 * 60 * 10; // 1 second * 60 * 10 = 10 minutes. | |
appLayout.bindConnectionMessage(); | |
} | |
appLayout.connection.start() | |
.then(function() { | |
appLayout.onSignalrConnected(); | |
}) | |
.catch(function(error) { | |
appLayout.onSignalrConnectFailed(error); | |
}); | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment