/// <summary> /// Creates a new AMQP connection from JSON data. /// </summary> /// <param name="json">The JSON data to create the object from.</param> public static AmqpConnection FromJsonObject(JSONObject json) { var c = new AmqpConnection(); c.Name = json["Name"].Value; c.Host = json["Host"].Value; c.AmqpPort = json["AmqpPort"].AsInt; c.WebPort = json["WebPort"].AsInt; c.VirtualHost = json["VirtualHost"].Value; c.Username = json["Username"].Value; c.Password = json["Password"].Value; c.ReconnectInterval = (short)json["ReconnectInterval"].AsInt; c.RequestedHeartBeat = (ushort)json["RequestedHeartBeat"].AsInt; return(c); }
// Saves the current connections to disk static void SaveConfiguration(bool createConnection = true) { // Check to see if the current values are for a new connection var isNew = true; foreach (var c in connections) { // This is not a new connection... if (c.Name == Name) { isNew = false; break; } } // If this is a new connection, add it to the list before writing to disk if (createConnection && isNew) { var c = new AmqpConnection(); c.Name = Name; c.Host = Host; c.AmqpPort = AmqpPort; c.WebPort = WebPort; c.VirtualHost = VirtualHost; c.Username = Username; c.Password = Password; c.ReconnectInterval = ReconnectInterval; c.RequestedHeartBeat = RequestedHeartBeat; connections.Add(c); } // Serialize and save to disk try { var config = new AmqpConfiguration(); config.Connections = connections.ToArray(); File.WriteAllText(ConfigurationFilename, JsonUtility.ToJson(config, true)); LoadConfiguration(true); } catch (Exception ex) { Debug.LogErrorFormat("{0}", ex); } }
/// <summary> /// Loads AMQP configuration from disk. /// </summary> /// <param name="refreshAssets">When True, a refresh of the asset database will be forced.</param> public static void LoadConfiguration(bool refreshAssets = false) { // Ensure that a AMQP configuration file exists EnsureConfigurationFile(); // Look for connections file var filename = ConfigurationFilename; connections.Clear(); if (refreshAssets) { // Update Unity assets (needed to get the editor to see these new assets) AssetDatabase.Refresh(); } if (File.Exists(filename)) { // Parse connection JSON data try { var jsonText = File.ReadAllText(filename); var config = JSON.Parse(jsonText).AsObject; var jsonConnections = config["Connections"].AsArray; // Populate the connection list from the ata for (int i = 0; i < jsonConnections.Count; i++) { connections.Add(AmqpConnection.FromJsonObject(jsonConnections[i].AsObject)); } } catch (Exception ex) { Debug.LogErrorFormat("{0}", ex); } } }