Example #1
0
        public void Send(object body, string destination, IDictionary <string, string> headers)
        {
            if (State != Open)
            {
                throw new InvalidOperationException("The current state of the connection is not Open.");
            }

            var jsonPayload = JsonConvert.SerializeObject(body);

            headers.Add("destination", destination);
            headers.Add("content-type", "application/json;charset=UTF-8");
            headers.Add("content-length", Encoding.UTF8.GetByteCount(jsonPayload).ToString());
            var connectMessage = new StompMessage(StompCommand.Send, jsonPayload, headers);

            socket.Send(stompSerializer.Serialize(connectMessage));
        }
Example #2
0
        public void Connect(IDictionary <string, string> headers)
        {
            if (State != Closed)
            {
                throw new InvalidOperationException("The current state of the connection is not Closed.");
            }

            socket.Connect();

            var connectMessage = new StompMessage(StompCommand.Connect, headers);

            socket.Send(stompSerializer.Serialize(connectMessage));
            // todo: check response

            socket.OnMessage += HandleMessage;
            State             = Open;
        }
Example #3
0
        public void Subscribe <T>(string topic, IDictionary <string, string> headers, EventHandler <T> handler)
        {
            if (State != Open)
            {
                throw new InvalidOperationException("The current state of the connection is not Open.");
            }

            headers.Add("destination", topic);
            headers.Add("id", "stub"); // todo: study and implement
            var subscribeMessage = new StompMessage(StompCommand.Subscribe, headers);

            socket.Send(stompSerializer.Serialize(subscribeMessage));
            // todo: check response
            // todo: implement advanced topic
            var sub = new Subscriber((sender, body) => handler(this, (T)body), typeof(T));

            subscribers.Add(topic, sub);
        }
        public string Serialize(StompMessage message)
        {
            var buffer = new StringBuilder();

            buffer.Append(message.Command + "\n");

            if (message.Headers != null)
            {
                foreach (var header in message.Headers)
                {
                    buffer.Append(header.Key + ":" + header.Value + "\n");
                }
            }

            buffer.Append("\n");
            buffer.Append(message.Body);
            buffer.Append('\0');

            return(buffer.ToString());
        }