Created
September 20, 2019 16:25
-
-
Save nCally/653ab8a45fc6537748357dd1b1249245 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* eslint-disable */ | |
| import $ from "jquery" | |
| import { HubConnectionBuilder } from "@aspnet/signalr/dist/esm/HubConnectionBuilder"; | |
| import * as Logger from "@aspnet/signalr/dist/esm/Loggers" | |
| import { LogLevel } from "@aspnet/signalr/dist/esm/ILogger"; | |
| export default class SignalRPlugin { | |
| static connection = null; | |
| static isConnected = false; | |
| static connectedEvent = undefined; | |
| static disconnectedEvent = undefined; | |
| startConnection(connection, connectedEvent, disconnectedEvent) { | |
| if (SignalRPlugin.isConnected === false) { | |
| connection.start().then(() => { | |
| SignalRPlugin.isConnected = true; | |
| connection.onclose(() => { | |
| SignalRPlugin.isConnected = false; | |
| if (disconnectedEvent !== undefined) | |
| disconnectedEvent(); | |
| try { | |
| setTimeout(() => | |
| this.startConnection(connection, connectedEvent, disconnectedEvent), | |
| 10000); | |
| } catch (err) { | |
| console.log(err); | |
| setTimeout(() => | |
| this.startConnection(connection, connectedEvent, disconnectedEvent), | |
| 10000); | |
| } | |
| }); | |
| }).catch((err) => { | |
| console.error(err.toString()); | |
| setTimeout(() => | |
| this.startConnection(connection, connectedEvent, disconnectedEvent), | |
| 10000); | |
| }); | |
| } | |
| SignalRPlugin.connection.on("SocketConnection", (status) => { | |
| if (status) { | |
| SignalRPlugin.connection.invoke("SendBalance") | |
| .catch(err => console.error(err.toString())); | |
| $("#connectionStatusId").html("Connected"); | |
| console.log("A client has connected"); | |
| /* eslint-disable */ | |
| connectedEvent(); | |
| } else { | |
| console.log("A client has disconnected"); | |
| /* eslint-disable */ | |
| disconnectedEvent(); | |
| } | |
| }); | |
| } | |
| registerConnectionForEvent() { | |
| SignalRPlugin.isConnected = true; | |
| if (SignalRPlugin.connectedEvent !== undefined) | |
| SignalRPlugin.connectedEvent(); | |
| } | |
| connect(connectedEvent, disconnectedEvent) { | |
| this.connectedEvent = connectedEvent; | |
| this.disconnectedEvent = disconnectedEvent; | |
| SignalRPlugin.connectedEvent = connectedEvent; | |
| SignalRPlugin.disconnectedEvent = disconnectedEvent; | |
| SignalRPlugin.connection = new HubConnectionBuilder() | |
| .withUrl("/MainHub") | |
| .configureLogging(LogLevel.Information) | |
| .build(); | |
| this.startConnection(SignalRPlugin.connection, connectedEvent, disconnectedEvent); | |
| } | |
| constructor() { | |
| } | |
| registerLiveTransactionEvent(liveTransactionEvent) { | |
| //SocketTransaction is called when a transaction occurs | |
| SignalRPlugin.connection.on("SocketTransaction", (transactionModel) => { | |
| liveTransactionEvent(transactionModel); | |
| this.registerConnectionForEvent(); | |
| }); | |
| } | |
| registerFlutterwaveBalanceEvent(updateFlutterwaveBalanceEvent) { | |
| SignalRPlugin.connection.on("GetFlutterWaveWalletBalance", (balance) => { | |
| console.log({ flutterwavebalance: balance }); | |
| updateFlutterwaveBalanceEvent(balance); | |
| this.registerConnectionForEvent(); | |
| }); | |
| } | |
| registerPayStackWalletBalance(updatePayStackBalanceEvent) { | |
| console.log({ "conn": SignalRPlugin.connection }) | |
| SignalRPlugin.connection.on("GetPayStackWalletBalance", (balance) => { | |
| console.log({ "paystackBalance": balance }); | |
| updatePayStackBalanceEvent(balance) | |
| this.registerConnectionForEvent(); | |
| }) | |
| } | |
| // Events for different transactions | |
| registerNewFundingTransaction(addNewFundingTransaction) { | |
| SignalRPlugin.connection.on("SocketFundingTransaction", (transaction) => { | |
| addNewFundingTransaction(transaction) | |
| this.registerConnectionForEvent(); | |
| }) | |
| } | |
| registerNewTransferTransaction(addNewTransferTransaction) { | |
| SignalRPlugin.connection.on("SocketTransaction", (transaction) => { | |
| addNewTransferTransaction(transaction) | |
| this.registerConnectionForEvent(); | |
| }) | |
| } | |
| registerNewCashoutTransaction(addNewCashoutTransaction) { | |
| SignalRPlugin.connection.on("SocketWithdrawalTransaction", (transaction) => { | |
| addNewCashoutTransaction(transaction) | |
| this.registerConnectionForEvent(); | |
| }) | |
| } | |
| // Event for average transactions amount | |
| registerNewAverageTransactions(addNewAverageTransaction) { | |
| SignalRPlugin.connection.on("SyncAverageTransactionsPerHour", (transaction) => { | |
| console.log({ average: transaction }); | |
| addNewAverageTransaction(transaction) | |
| this.registerConnectionForEvent(); | |
| }) | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment