123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using System;
- using System.Collections.Generic;
- namespace BestHTTP.SignalRCore.Messages
- {
- public sealed class SupportedTransport
- {
- /// <summary>
- /// Name of the transport.
- /// </summary>
- public string Name { get; private set; }
- /// <summary>
- /// Supported transfer formats of the transport.
- /// </summary>
- public List<string> SupportedFormats { get; private set; }
- internal SupportedTransport(string transportName, List<string> transferFormats)
- {
- this.Name = transportName;
- this.SupportedFormats = transferFormats;
- }
- }
- /// <summary>
- /// Negotiation result of the /negotiation request.
- /// <see cref="https://github.com/aspnet/SignalR/blob/dev/specs/TransportProtocols.md#post-endpoint-basenegotiate-request"/>
- /// </summary>
- public sealed class NegotiationResult
- {
- /// <summary>
- /// The connectionId which is required by the Long Polling and Server-Sent Events transports (in order to correlate sends and receives).
- /// </summary>
- public string ConnectionId { get; private set; }
- /// <summary>
- /// The availableTransports list which describes the transports the server supports. For each transport, the name of the transport (transport) is listed, as is a list of "transfer formats" supported by the transport (transferFormats)
- /// </summary>
- public List<SupportedTransport> SupportedTransports { get; private set; }
- /// <summary>
- /// The url which is the URL the client should connect to.
- /// </summary>
- public Uri Url { get; private set; }
- /// <summary>
- /// The accessToken which is an optional bearer token for accessing the specified url.
- /// </summary>
- public string AccessToken { get; private set; }
- internal static NegotiationResult Parse(string json, out string error)
- {
- error = null;
- Dictionary<string, object> response = BestHTTP.JSON.Json.Decode(json) as Dictionary<string, object>;
- if (response == null)
- {
- error = "Json decoding failed!";
- return null;
- }
- try
- {
- NegotiationResult result = new NegotiationResult();
- object value;
- if (response.TryGetValue("connectionId", out value))
- result.ConnectionId = value.ToString();
- if (response.TryGetValue("availableTransports", out value))
- {
- List<object> transports = value as List<object>;
- if (transports != null)
- {
- List<SupportedTransport> supportedTransports = new List<SupportedTransport>(transports.Count);
- foreach (Dictionary<string, object> transport in transports)
- {
- string transportName = string.Empty;
- List<string> transferModes = null;
- if (transport.TryGetValue("transport", out value))
- transportName = value.ToString();
- if (transport.TryGetValue("transferFormats", out value))
- {
- List<object> transferFormats = value as List<object>;
- if (transferFormats != null)
- {
- transferModes = new List<string>(transferFormats.Count);
- foreach (var mode in transferFormats)
- transferModes.Add(mode.ToString());
- }
- }
- supportedTransports.Add(new SupportedTransport(transportName, transferModes));
- }
- result.SupportedTransports = supportedTransports;
- }
- }
- if (response.TryGetValue("url", out value))
- result.Url = new Uri(value.ToString());
- if (response.TryGetValue("accessToken", out value))
- result.AccessToken = value.ToString();
- return result;
- }
- catch (Exception ex)
- {
- error = "Error while parsing result: " + ex.Message + " StackTrace: " + ex.StackTrace;
- return null;
- }
- }
- }
- }
|