Example #1
0
        /// <summary>
        /// Example to demonstrate STOMP transactions using the StompNet low level API.
        ///
        /// In this example, a single message is going to be sent to a queue and it will be read from
        /// the same queue.
        /// </summary>
        public static async Task ExampleWriterAndReader()
        {
            using (TcpClient tcpClient = new TcpClient(serverHostname, serverPort))

                // Create a frame writer and frame reader. These classes are NOT thread safe.
                // Stomp12FrameWriter and Stomp12FrameReader are NOT thread safety. They could
                // wrapped into a StompSerialFrameWriter and a StompSerialFrameReader for
                // serializing operations (thread-safe).
                using (IStompFrameWriter writer = new Stomp12FrameWriter(tcpClient.GetStream()))
                    using (IStompFrameReader reader = new Stomp12FrameReader(tcpClient.GetStream()))
                    {
                        Frame inFrame;

                        //---------------------------------
                        // Write CONNECT.
                        //
                        await writer.WriteConnectAsync(virtualHost, login, passcode);

                        // Read CONNECTED.
                        // Keep reading while receiving Heartbeats.
                        do
                        {
                            inFrame = await reader.ReadFrameAsync();
                        } while (inFrame.Command == StompCommands.Heartbeat);

                        //Verify a CONNECTED frame was received.
                        if (!AssertExpectedCommandFrame(inFrame, StompCommands.Connected))
                        {
                            return;
                        }

                        Console.WriteLine("Connected");


                        //---------------------------------
                        // Write SEND command with receipt.
                        //
                        await writer.WriteSendAsync(
                            aQueueName,
                            messageContent + (" (WITH RECEIPT)."),
                            "myreceiptid-123");

                        // Read RECEIPT.
                        // Keep reading while receiving Heartbeats.
                        do
                        {
                            inFrame = await reader.ReadFrameAsync();
                        } while (inFrame.Command == StompCommands.Heartbeat);

                        //Verify a RECEIPT frame was received.
                        if (!AssertExpectedCommandFrame(inFrame, StompCommands.Receipt))
                        {
                            return;
                        }

                        // Process incoming RECEIPT.
                        // Using the incoming frame, Interpret will create a new instance of a sub-class
                        // of Frame depending on the Command. If the frame is a HEARTBEAT, the same frame
                        // will be returned. Possible sub-classes: ConnectedFrame, ErrorFrame, MessageFrame,
                        // ReceiptFrame.
                        // Interpret throws exception if the incoming frame is malformed (not standard).
                        ReceiptFrame rptFrame = StompInterpreter.Interpret(inFrame) as ReceiptFrame;

                        if (rptFrame.ReceiptId != "myreceiptid-123")
                        {
                            Console.WriteLine("ERROR: Unexpected receipt " + rptFrame.ReceiptId + ".");
                            return;
                        }

                        Console.WriteLine("Received matching receipt.");

                        //---------------------------------
                        // Write SUBSCRIBE.
                        //
                        string subscriptionId = new Random().Next().ToString();
                        await writer.WriteSubscribeAsync(aQueueName, subscriptionId, ack : StompAckValues.AckAutoValue);

                        // Read MESSAGE.
                        // Keep reading while receiving Heartbeats.
                        do
                        {
                            inFrame = await reader.ReadFrameAsync();
                        } while (inFrame.Command == StompCommands.Heartbeat);

                        //Verify a MESSAGE frame was received.
                        if (!AssertExpectedCommandFrame(inFrame, StompCommands.Message))
                        {
                            return;
                        }

                        // Process incoming RECEIPT.
                        MessageFrame msgFrame = StompInterpreter.Interpret(inFrame) as MessageFrame;
                        Console.WriteLine("Received Message:");
                        Console.WriteLine();
                        Console.WriteLine("Destination: " + msgFrame.Destination);
                        Console.WriteLine("ContentType: " + msgFrame.ContentType);
                        Console.WriteLine("ContentLength: " + msgFrame.ContentLength);
                        Console.WriteLine("Content:" + msgFrame.GetBodyAsString());
                        Console.WriteLine();

                        // Write DISCONNECT.
                        await writer.WriteDisconnectAsync();

                        Console.WriteLine("Disconnected.");
                    }
        }
 public ReceiptReceivedEventArgs(ReceiptFrame receiptFrame)
 {
     this.ReceiptFrame = receiptFrame;
 }