//****************************************************************************************************** // HubClientBase.cs - Gbtc // // Copyright © 2016, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this // file except in compliance with the License. You may obtain a copy of the License at: // // http://opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 08/16/2016 - Ritchie Carroll // Generated original version of source code. // //****************************************************************************************************** using System; using Microsoft.AspNet.SignalR.Hubs; namespace GSF.Web.Hubs { /// /// Represents a base class implementation for implementations. /// /// /// This base class is useful for defining classes that should persist per SignalR session. /// public abstract class HubClientBase : IHubClient { #region [ Members ] // Fields private dynamic m_clientScript; private bool m_disposed; #endregion #region [ Properties ] /// /// Gets reference to SignalR hub client browser DOM functionality. /// /// /// This property can be used to call registered Javascript hub functions. /// public dynamic ClientScript => m_clientScript ?? (m_clientScript = HubInstance?.Clients?.Client(ConnectionID)); /// /// Gets or sets reference to SignalR hub instance. /// public IHub HubInstance { get; set; } /// /// Gets or sets the connection ID of the calling client. /// public string ConnectionID { get; set; } /// /// Gets or sets delegate to use to log status messages. /// public Action LogStatusMessageFunction { get; set; } /// /// Gets or sets delegate to use to log exception messages. /// public Action LogExceptionFunction { get; set; } #endregion #region [ Methods ] /// /// Releases all the resources used by the object. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Releases the unmanaged resources used by the object and optionally releases the managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected virtual void Dispose(bool disposing) { if (!m_disposed) { try { if (disposing) { m_clientScript = null; HubInstance = null; LogStatusMessageFunction = null; LogExceptionFunction = null; } } finally { m_disposed = true; // Prevent duplicate dispose. } } } /// /// Logs a status message to hub client and any provided log status message delegate. /// /// Message content to log. /// Type of message to log. /// Determines if message should be logged to hub client. /// /// When is true, function will /// call "sendInfoMessage" Javascript hub client function if defined. /// protected void LogStatusMessage(string message, UpdateType type = UpdateType.Information, bool logToClient = true) { // Send status message to hub client if (logToClient) ClientScript?.sendInfoMessage(message, type == UpdateType.Information ? 2000 : -1); // Send status message to program host LogStatusMessageFunction?.Invoke(message, type); } /// /// Logs an exception to hub client and any provided log exception delegate. /// /// Exception to log. /// Determines if message should be logged to hub client. /// /// When is true, function will /// call "sendErrorMessage" Javascript hub client function if defined. /// protected void LogException(Exception ex, bool logToClient = true) { // Send exception to hub client if (logToClient) ClientScript?.sendErrorMessage(ex.Message, -1); // Send exception to program host LogExceptionFunction?.Invoke(ex); } #endregion } }