parent
04c5230b6d
commit
4cb28e49de
@ -0,0 +1,52 @@ |
|||||||
|
import {HubConnectionBuilder, LogLevel} from "@aspnet/signalr"; |
||||||
|
import Vue from "vue"; |
||||||
|
|
||||||
|
export default class TemperatureHub |
||||||
|
{ |
||||||
|
hubConnection; |
||||||
|
|
||||||
|
constructor() { |
||||||
|
this.hubConnection = new HubConnectionBuilder() |
||||||
|
.withUrl(process.env.VUE_APP_TEMPERATURE_HUB) |
||||||
|
.configureLogging(LogLevel.Information) |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
start() { |
||||||
|
let startedPromise = null; |
||||||
|
let that = this; |
||||||
|
startedPromise = this.hubConnection.start().catch(() => { |
||||||
|
return new Promise((resolve, reject) => |
||||||
|
setTimeout(() => that.start().then(resolve).catch(reject), process.env.VUE_APP_TIMEOUT_TEMPERATURE_HUB)) |
||||||
|
}) |
||||||
|
return startedPromise; |
||||||
|
} |
||||||
|
|
||||||
|
startHub() |
||||||
|
{ |
||||||
|
let that = this; |
||||||
|
this.hubConnection.onclose(() => |
||||||
|
{ |
||||||
|
that.start(); |
||||||
|
}/**/); |
||||||
|
|
||||||
|
this.start(); |
||||||
|
|
||||||
|
// use new Vue instance as an event bus
|
||||||
|
let questionHub = new Vue() |
||||||
|
// every component will use this.$questionHub to access the event bus
|
||||||
|
|
||||||
|
// Forward server side SignalR events through $questionHub, where components will listen to them
|
||||||
|
this.hubConnection.on(process.env.VUE_APP_MSG_TEMPERATURE_HUB, (msg) => { |
||||||
|
questionHub.$emit(process.env.VUE_APP_EMIT_MSG_TEMPERATURE, JSON.parse(msg) ) |
||||||
|
}) |
||||||
|
|
||||||
|
return questionHub; |
||||||
|
} |
||||||
|
|
||||||
|
async stopHub() |
||||||
|
{ |
||||||
|
await this.hubConnection.stop(); |
||||||
|
|
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue