public void StonksMessageSystemSetup(string usrnm = "a", string pwd = "a") { username = usrnm; pass = pwd; Factory = new ConnectionFactory(); Factory.UserName = "******"; Factory.Password = "******"; Factory.VirtualHost = "SimpleVirtHost"; Factory.HostName = "localhost"; connection = Factory.CreateConnection(); channel = connection.CreateModel(); channel.ExchangeDeclare(Factory.VirtualHost, ExchangeType.Direct); channel.QueueDeclare(queue: "stocks", false, false, false, null); //position StocksMessageStruct temp = new StocksMessageStruct(); temp.type = StocksEnumMessageType.MarketPrice; //enum type for message temp.Message = "Stocks messaging system active!"; string JsonEncoded = JsonUtility.ToJson(temp); var body = Encoding.UTF8.GetBytes(JsonEncoded); channel.BasicPublish(exchange: "", routingKey: "stocks", basicProperties: null, body: body); //posotion }
public void Setup(string UsrName, string pwd, string Host, string VHost) { Username = UsrName; Password = pwd; ConnectionHost = Host; ConnectionVirtualHost = VHost; TextBoxSend.text = "Type a message!"; factory = new ConnectionFactory(); factory.UserName = Username; factory.Password = Password; if (ConnectionVirtualHost.Length != 0) { factory.VirtualHost = ConnectionVirtualHost; } else { factory.VirtualHost = "SimpleVirtHost"; } if (ConnectionHost.Length != 0) { factory.HostName = Host; } else { factory.HostName = "localhost"; } conn = factory.CreateConnection(); channel = conn.CreateModel(); /*above code uses default user & settings, can use -user- provided login details * * // this name will be shared by all connections instantiated by * // this factory * factory.ClientProvidedName = "app:audit component:event-consumer" * * * building blocks of protocol must be defined and linked together before they can be used */ channel.ExchangeDeclare(factory.VirtualHost, ExchangeType.Direct); channel.QueueDeclare(queue: "hello", false, false, false, null); SimpleMessage Message = new SimpleMessage(); //fill temporary message Message.Message = "Hello world!"; //create the simplest hello world message Message.Username = Username; //fill the username out for sending string JsonEncoded = JsonUtility.ToJson(Message); //serialize to JSON for complex data sending var body = Encoding.UTF8.GetBytes(JsonEncoded); //utf-8 encoding for send channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body); //send the message List_MessageCollection = new List <SimpleMessage>(); // above is all the setup needed to send a message on a single channel -- creating one igf there's not one provided. consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body2 = ea.Body.ToArray(); var message2 = Encoding.UTF8.GetString(body2); //decode from UTF-8; need to de-serialze JSON message format SimpleMessage recievedMessage = JsonUtility.FromJson <SimpleMessage>(message2); List_MessageCollection.Add(recievedMessage); //add to list to print to screen }; channel.BasicConsume(queue: "hello", autoAck: true, consumer: consumer); //Debug.Log("Simple tasks finished, necking."); //Tiling message rec system in progress TilingChannel = conn.CreateModel(); TilingChannel.ExchangeDeclare(factory.VirtualHost, ExchangeType.Direct); TilingChannel.QueueDeclare(queue: "position", false, false, false, null); List_TilingMessages_Collision = new List <TilingMessage>(); List_TilingMessages_PositionUpdates = new List <TilingMessage>(); TilingConsumer = new EventingBasicConsumer(TilingChannel); TilingConsumer.Received += (model, ea) => { var body2 = ea.Body.ToArray(); var Message2 = Encoding.UTF8.GetString(body2); TilingMessage temp = JsonUtility.FromJson <TilingMessage>(Message2); if (temp.MessageType == messageClass.querySend) { List_TilingMessages_Collision.Add(temp); } else if (temp.MessageType == messageClass.positionUpdate) { List_TilingMessages_PositionUpdates.Add(temp); } }; TilingChannel.BasicConsume(queue: "position", autoAck: true, consumer: TilingConsumer); //stocks messaging system StocksChannel = conn.CreateModel(); //create chanel setup StocksChannel.ExchangeDeclare(factory.VirtualHost, ExchangeType.Direct); //channel decleration type StocksChannel.QueueDeclare(queue: "stocks", false, false, false, null); List_StocksMessaging_MarketPrice = new List <StocksMessageStruct>(); //fill list List_StocksMessaging_Sell_n_BuyRequests = new List <StocksMessageStruct>(); //fill list StocksConsumer = new EventingBasicConsumer(StocksChannel); //create consumer system StocksConsumer.Received += (model, ea) => { var Body = ea.Body.ToArray(); var StocksMessage = Encoding.UTF8.GetString(Body); StocksMessageStruct temp = JsonUtility.FromJson <StocksMessageStruct>(StocksMessage); if (temp.type == StocksEnumMessageType.MarketPrice) { List_StocksMessaging_MarketPrice.Add(temp); } if (temp.type == StocksEnumMessageType.buyReq || temp.type == StocksEnumMessageType.sellReq) { List_StocksMessaging_Sell_n_BuyRequests.Add(temp); } }; StocksChannel.BasicConsume(queue: "stocks", autoAck: true, consumer: StocksConsumer); }