Ejemplo n.º 1
0
        public static HandshakeResponseMessage ParseResponseMessage(ReadOnlyMemory <byte> payload)
        {
            using (var reader = CreateJsonTextReader(payload))
            {
                var token            = JToken.ReadFrom(reader);
                var handshakeJObject = JsonUtils.GetObject(token);

                // a handshake response does not have a type
                // check the incoming message was not any other type of message
                var type = JsonUtils.GetOptionalProperty <string>(handshakeJObject, TypePropertyName);
                if (!string.IsNullOrEmpty(type))
                {
                    throw new InvalidOperationException("Handshake response should not have a 'type' value.");
                }

                var error = JsonUtils.GetOptionalProperty <string>(handshakeJObject, ErrorPropertyName);
                return(new HandshakeResponseMessage(error));
            }
        }
Ejemplo n.º 2
0
        public static bool TryParseRequestMessage(ReadOnlySequence <byte> buffer, out HandshakeRequestMessage requestMessage, out SequencePosition consumed, out SequencePosition examined)
        {
            if (!TryReadMessageIntoSingleMemory(buffer, out consumed, out examined, out var memory))
            {
                requestMessage = null;
                return(false);
            }

            if (!TextMessageParser.TryParseMessage(ref memory, out var payload))
            {
                throw new InvalidDataException("Unable to parse payload as a handshake request message.");
            }

            using (var reader = CreateJsonTextReader(payload))
            {
                var token            = JToken.ReadFrom(reader);
                var handshakeJObject = JsonUtils.GetObject(token);
                var protocol         = JsonUtils.GetRequiredProperty <string>(handshakeJObject, ProtocolPropertyName);
                requestMessage = new HandshakeRequestMessage(protocol);
            }

            return(true);
        }