#if !BESTHTTP_DISABLE_SOCKETIO using System; using System.Collections.Generic; namespace BestHTTP.SocketIO { using BestHTTP.JSON; /// /// Helper class to parse and hold handshake information. /// public sealed class HandshakeData { #region Public Handshake Data /// /// Session ID of this connection. /// public string Sid { get; private set; } /// /// List of possible upgrades. /// public List Upgrades { get; private set; } /// /// What interval we have to set a ping message. /// public TimeSpan PingInterval { get; private set; } /// /// What time have to pass without an answer to our ping request when we can consider the connection disconnected. /// public TimeSpan PingTimeout { get; private set; } #endregion #region Helper Methods public bool Parse(string str) { bool success = false; Dictionary dict = Json.Decode(str, ref success) as Dictionary; if (!success) return false; try { this.Sid = GetString(dict, "sid"); this.Upgrades = GetStringList(dict, "upgrades"); this.PingInterval = TimeSpan.FromMilliseconds(GetInt(dict, "pingInterval")); this.PingTimeout = TimeSpan.FromMilliseconds(GetInt(dict, "pingTimeout")); } catch (Exception ex) { BestHTTP.HTTPManager.Logger.Exception("HandshakeData", "Parse", ex); return false; } return true; } private static object Get(Dictionary from, string key) { object value; if (!from.TryGetValue(key, out value)) throw new System.Exception(string.Format("Can't get {0} from Handshake data!", key)); return value; } private static string GetString(Dictionary from, string key) { return Get(from, key) as string; } private static List GetStringList(Dictionary from, string key) { List value = Get(from, key) as List; List result = new List(value.Count); for (int i = 0; i < value.Count; ++i) { string str = value[i] as string; if (str != null) result.Add(str); } return result; } private static int GetInt(Dictionary from, string key) { return (int)(double)Get(from, key); } #endregion } } #endif