Exemple #1
0
        public Task Send <T>(T body, string destination, IDictionary <string, string> headers)
        {
            if (State != StompConnectionStatus.Connected)
            {
                throw new InvalidOperationException("The stomp client is not connected.");
            }

            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 StompFrame(StompFrameKind.Send, jsonPayload, headers);

            _socket.Send(_stompSerializer.Serialize(connectMessage));
            return(Task.CompletedTask);
        }
        public Task Subscribe <T>(string topic, IDictionary <string, string> headers, EventHandler <T> handler)
        {
            if (State != StompConnectionStatus.Connected)
            {
                throw new InvalidOperationException("The stomp client is not connected.");
            }

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

            _tcpClient.Client.Send(_encode.GetBytes(_stompSerializer.Serialize(subscribeMessage)));

            var sub = new Subscriber((sender, body) => handler(this, (T)body), typeof(T));

            _subscribers.Add(topic, sub);
            return(Task.CompletedTask);
        }
        public async Task Send <T>(T body, string destination, IDictionary <string, string> headers)
        {
            if (State != StompConnectionStatus.Connected)
            {
                throw new InvalidOperationException("The stomp client is not connected.");
            }

            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 message = new StompFrame(StompFrameKind.Send, jsonPayload, headers);

            _sendTimeout.Start();
            await _tcpClient.Client.SendAsync(_encode.GetBytes(_stompSerializer.Serialize(message)), SocketFlags.None);

            _sendTimeout.Stop();
        }
Exemple #4
0
        /// <summary>
        /// Serializes the frame into a STOMP protocol compatible string.
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public string Serialize(StompFrame message)
        {
            var buffer = new StringBuilder();

            buffer.Append(message.FrameKind + "\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());
        }
Exemple #5
0
        public Task Connect(IDictionary <string, string> headers)
        {
            if (State != StompConnectionStatus.NeverConnected && State != StompConnectionStatus.DisconnectedByHost && State != StompConnectionStatus.DisconnectedByUser)
            {
                throw new InvalidOperationException("The current state of the connection is not Closed.");
            }

            State = StompConnectionStatus.Connecting;
            _socket.Connect();

            var connectMessage = new StompFrame(StompFrameKind.Connect, headers);

            _socket.Send(_stompSerializer.Serialize(connectMessage));

            _socket.OnMessage += HandleMessage;
            _socket.OnError   += (object sender, ErrorEventArgs e) =>
            {
                State = StompConnectionStatus.Error;
                OnError?.Invoke(this, e.Message);
            };

            _socket.OnClose += (object sender, CloseEventArgs e) =>
            {
                State = StompConnectionStatus.DisconnectedByHost;
                OnDisconnected?.Invoke(this, e.Reason);
            };

            _socket.OnOpen += (object sender, EventArgs e) =>
            {
                State = StompConnectionStatus.Connected;
                OnConnected?.Invoke(this, null);
            };

            State = StompConnectionStatus.Connected;
            return(Task.CompletedTask);
        }