Ejemplo n.º 1
0
        public async Task SendMessageAsync(object messsage)
        {
            // Allow to send command/response/event/acknowledgement type of mesage derived from MessageBase class
            MessageBase messageBase = messsage as MessageBase;

            messageBase.IsNotNull($"Unexpected type of object received to serialize message in the ${nameof(SendMessageAsync)} method.");

            try
            {
                string JSON = messageBase.Serialise();
                Logger.LogSensitive(Constants.Component, $"Sending: {ProcessDataTypes(messsage)}");

                await socket.SendAsync(new ArraySegment <byte>(Encoding.UTF8.GetBytes(JSON)), WebSocketMessageType.Text, true, CancellationToken.None);
            }
            catch (ObjectDisposedException)
            {
                Logger.Warning(Constants.Component, "The ClientWebSocket has been closed");
            }
            catch (InvalidOperationException)
            {
                Logger.Warning(Constants.Component, "The ClientWebSocket is not connected");
            }
            catch (InvalidDataException ex)
            {
                Contracts.Fail($"Invalid data is set by the device class.{messageBase.Headers.Name}. {ex.Message}");
            }
            catch (Exception ex)
            {
                Contracts.Fail($"Exception caught while in serialising JSON on sending message. {ex.Message}");
            }
        }