/// <summary> /// Publishes a message to the current exchange using the form's input values. /// </summary> public void Publish() { // Validate args var isValid = true; var exchangeName = PublishExchange.options[PublishExchange.value].text; if (string.IsNullOrEmpty(exchangeName)) { isValid = false; AmqpConsole.Color = Color.red; AmqpConsole.WriteLine("* Exchange Name cannot be blank"); AmqpConsole.Color = null; } var message = PublishMessage.text; if (string.IsNullOrEmpty(message)) { isValid = false; AmqpConsole.Color = Color.red; AmqpConsole.WriteLine("* Message cannot be blank"); AmqpConsole.Color = null; } // Don't continue if values are invald if (!isValid) { return; } var exchangeType = AmqpExchangeTypes.Direct; // Find this exchange and get its exchange type foreach (var exchange in exchanges) { if (exchange.Name == exchangeName) { exchangeType = exchange.Type; break; } } var routingKey = PublishRoutingKey.text; // Publish the message AmqpClient.Publish(exchangeName, routingKey, message); PublishMessage.text = null; // clear out message // Refocus the message area PublishMessage.Select(); PublishMessage.ActivateInputField(); }
/// <summary> /// Subscribes to the AMQP exchange subscription using the form's values. /// </summary> public void Subscribe() { // Validate args var isValid = true; var exchangeName = ExchangeName.options[ExchangeName.value].text; if (string.IsNullOrEmpty(exchangeName)) { isValid = false; AmqpConsole.Color = Color.red; AmqpConsole.WriteLine("* Exchange Name cannot be blank"); AmqpConsole.Color = null; } // Don't continue if values are invald if (!isValid) { return; } var exchangeType = AmqpExchangeTypes.Direct; // Find this exchange and get its exchange type foreach (var exchange in exchanges) { if (exchange.Name == exchangeName) { exchangeType = exchange.Type; break; } } var routingKey = RoutingKey.text; // Ensure this subscription doesn't already exist foreach (var sub in exSubscriptions) { if (sub.ExchangeName == exchangeName && sub.ExchangeType == exchangeType && sub.RoutingKey == routingKey) { AmqpConsole.Color = new Color(1f, 0.5f, 0); AmqpConsole.WriteLineFormat("Subscription already exists for exchange {0}:{1}", exchangeName, routingKey); AmqpConsole.Color = null; return; } } // Create the new subscription var subscription = new UnityAmqpExchangeSubscription(exchangeName, exchangeType, routingKey, null, AmqpClient.Instance.UnityEventDebugExhangeMessageHandler); // Subscribe on the client AmqpClient.Subscribe(subscription); }
/// <summary> /// Unsubscribes from the AMQP exchange subscription using the form's values. /// </summary> public void Unsubscribe() { // Validate args var isValid = true; var exchangeName = ExchangeName.options[ExchangeName.value].text; if (string.IsNullOrEmpty(exchangeName)) { isValid = false; AmqpConsole.Color = Color.red; AmqpConsole.WriteLine("* Exchange Name cannot be blank"); AmqpConsole.Color = null; } // Don't continue if values are invald if (!isValid) { return; } var exchangeType = AmqpExchangeTypes.Direct; // Find this exchange and get its exchange type foreach (var exchange in exchanges) { if (exchange.Name == exchangeName) { exchangeType = exchange.Type; break; } } var routingKey = RoutingKey.text; // Ensure this subscription already exists var subs = exSubscriptions.ToArray(); foreach (var sub in subs) { if (sub.ExchangeName == exchangeName && sub.ExchangeType == exchangeType && sub.RoutingKey == routingKey) { AmqpClient.Unsubscribe(sub); exSubscriptions.Remove(sub); return; } } AmqpConsole.Color = new Color(1f, 0.5f, 0); AmqpConsole.WriteLineFormat("Subscription not found for exchange {0}:{1}", exchangeName, routingKey); AmqpConsole.Color = null; }