/// <summary>
        /// Writes the serialized representation of a <see cref="HandshakeResponseMessage"/> to the specified writer.
        /// </summary>
        /// <param name="responseMessage">The message to write.</param>
        /// <param name="output">The output writer.</param>
        public static void WriteResponseMessage(HandshakeResponseMessage responseMessage, IBufferWriter <byte> output)
        {
            var textWriter = Utf8BufferTextWriter.Get(output);

            try
            {
                using (var writer = JsonUtils.CreateJsonTextWriter(textWriter))
                {
                    writer.WriteStartObject();
                    if (!string.IsNullOrEmpty(responseMessage.Error))
                    {
                        writer.WritePropertyName(ErrorPropertyName);
                        writer.WriteValue(responseMessage.Error);
                    }

                    writer.WritePropertyName(MinorVersionPropertyName);
                    writer.WriteValue(responseMessage.MinorVersion);

                    writer.WriteEndObject();
                    writer.Flush();
                }
            }
            finally
            {
                Utf8BufferTextWriter.Return(textWriter);
            }

            TextMessageFormatter.WriteRecordSeparator(output);
        }
        private void WriteMessageCore(HubMessage message, IBufferWriter <byte> stream)
        {
            var textWriter = Utf8BufferTextWriter.Get(stream);

            try
            {
                using (var writer = JsonUtils.CreateJsonTextWriter(textWriter))
                {
                    writer.WriteStartObject();
                    switch (message)
                    {
                    case InvocationMessage m:
                        WriteMessageType(writer, HubProtocolConstants.InvocationMessageType);
                        WriteHeaders(writer, m);
                        WriteInvocationMessage(m, writer);
                        break;

                    default:
                        throw new InvalidOperationException($"Unsupported message type: {message.GetType().FullName}");
                    }
                    writer.WriteEndObject();
                    writer.Flush();
                }
            }
            finally
            {
                Utf8BufferTextWriter.Return(textWriter);
            }
        }
Exemple #3
0
        public static void WriteResponse(NegotiationResponse response, Stream output)
        {
            using (var jsonWriter = JsonUtils.CreateJsonTextWriter(new StreamWriter(output, _utf8NoBom, 1024, leaveOpen: true)))
            {
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName(ConnectionIdPropertyName);
                jsonWriter.WriteValue(response.ConnectionId);
                jsonWriter.WritePropertyName(AvailableTransportsPropertyName);
                jsonWriter.WriteStartArray();

                foreach (var availableTransport in response.AvailableTransports)
                {
                    jsonWriter.WriteStartObject();
                    jsonWriter.WritePropertyName(TransportPropertyName);
                    jsonWriter.WriteValue(availableTransport.Transport);
                    jsonWriter.WritePropertyName(TransferFormatsPropertyName);
                    jsonWriter.WriteStartArray();

                    foreach (var transferFormat in availableTransport.TransferFormats)
                    {
                        jsonWriter.WriteValue(transferFormat);
                    }

                    jsonWriter.WriteEndArray();
                    jsonWriter.WriteEndObject();
                }

                jsonWriter.WriteEndArray();
                jsonWriter.WriteEndObject();

                jsonWriter.Flush();
            }
        }
        public static void WriteResponse(NegotiationResponse response, IBufferWriter <byte> output)
        {
            var textWriter = Utf8BufferTextWriter.Get(output);

            try
            {
                using (var jsonWriter = JsonUtils.CreateJsonTextWriter(textWriter))
                {
                    jsonWriter.WriteStartObject();

                    if (!string.IsNullOrEmpty(response.Url))
                    {
                        jsonWriter.WritePropertyName(UrlPropertyName);
                        jsonWriter.WriteValue(response.Url);
                    }

                    if (!string.IsNullOrEmpty(response.AccessToken))
                    {
                        jsonWriter.WritePropertyName(AccessTokenPropertyName);
                        jsonWriter.WriteValue(response.AccessToken);
                    }

                    if (!string.IsNullOrEmpty(response.ConnectionId))
                    {
                        jsonWriter.WritePropertyName(ConnectionIdPropertyName);
                        jsonWriter.WriteValue(response.ConnectionId);
                    }

                    jsonWriter.WritePropertyName(AvailableTransportsPropertyName);
                    jsonWriter.WriteStartArray();

                    foreach (var availableTransport in response.AvailableTransports)
                    {
                        jsonWriter.WriteStartObject();
                        jsonWriter.WritePropertyName(TransportPropertyName);
                        jsonWriter.WriteValue(availableTransport.Transport);
                        jsonWriter.WritePropertyName(TransferFormatsPropertyName);
                        jsonWriter.WriteStartArray();

                        foreach (var transferFormat in availableTransport.TransferFormats)
                        {
                            jsonWriter.WriteValue(transferFormat);
                        }

                        jsonWriter.WriteEndArray();
                        jsonWriter.WriteEndObject();
                    }

                    jsonWriter.WriteEndArray();
                    jsonWriter.WriteEndObject();

                    jsonWriter.Flush();
                }
            }
            finally
            {
                Utf8BufferTextWriter.Return(textWriter);
            }
        }
        private async Task PublishAsync(string channel, IRedisMessage message)
        {
            byte[] payload;
            using (var stream = new LimitArrayPoolWriteStream())
                using (var writer = JsonUtils.CreateJsonTextWriter(new StreamWriter(stream)))
                {
                    _serializer.Serialize(writer, message);
                    writer.Flush();
                    payload = stream.ToArray();
                }

            _logger.PublishToChannel(channel);
            await _bus.PublishAsync(channel, payload);
        }
        /// <summary>
        /// Writes the serialized representation of a <see cref="HandshakeRequestMessage"/> to the specified writer.
        /// </summary>
        /// <param name="requestMessage">The message to write.</param>
        /// <param name="output">The output writer.</param>
        public static void WriteRequestMessage(HandshakeRequestMessage requestMessage, IBufferWriter <byte> output)
        {
            var textWriter = Utf8BufferTextWriter.Get(output);

            try
            {
                using (var writer = JsonUtils.CreateJsonTextWriter(textWriter))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName(ProtocolPropertyName);
                    writer.WriteValue(requestMessage.Protocol);
                    writer.WritePropertyName(ProtocolVersionPropertyName);
                    writer.WriteValue(requestMessage.Version);
                    writer.WriteEndObject();
                    writer.Flush();
                }
            }
            finally
            {
                Utf8BufferTextWriter.Return(textWriter);
            }

            TextMessageFormatter.WriteRecordSeparator(output);
        }