コード例 #1
0
        private void Disconnect(bool throwOnError)
        {
            try
            {
                var state = (StompConnectionState)Interlocked.CompareExchange(
                    ref _state,
                    value: (int)StompConnectionState.Disconnecting,
                    comparand: (int)StompConnectionState.Connected
                    );

                switch (state)
                {
                case StompConnectionState.Connected:
                    // Соединение было установлено, сейчас состояние - Disconnecting
                    // Текущий поток должен выполнить отключение
                    OnStateChanged(StompConnectionState.Disconnecting);

                    try
                    {
                        _transport?.Dispose();
                    }
                    catch (Exception)
                    {
                        if (throwOnError)
                        {
                            throw;
                        }
                    }
                    finally
                    {
                        _transport = null;
                        State      = StompConnectionState.Disconnected;
                    }
                    break;

                case StompConnectionState.Disconnecting:
                    // Соединение разрывается, следует дождаться изменения состояния
                    while (State == StompConnectionState.Disconnecting)
                    {
                    }
                    break;

                case StompConnectionState.Connecting:
                    if (throwOnError)
                    {
                        throw new StompConnectionException("Unable to disconnect while connecting");
                    }

                    return;

                default:
                    return;
                }
            }
            catch (Exception e)
            {
                _logger.Error("Error while disconnecting", e);
                throw;
            }
        }
コード例 #2
0
                #pragma warning restore 108

        public virtual void Disconnect()
        {
            if (stompConnectionState != StompConnectionState.Connected)
            {
                return;
            }

            stompConnectionState = StompConnectionState.Disconnecting;
            transmit(COMMAND_DISCONNECT, null, null);
        }
コード例 #3
0
 protected virtual void Dispose(Boolean disposing)
 {
     if (State != Open)
     {
         throw new InvalidOperationException(CONNECTION_STATE);
     }
     socket.Close();
     State = Closed;
     ((IDisposable)socket).Dispose();
 }
コード例 #4
0
        public void Dispose()
        {
            if (State != Open)
            {
                throw new InvalidOperationException("The current state of the connection is not Open.");
            }

            State = Closed;
            ((IDisposable)socket).Dispose();
            // todo: unsubscribe
        }
コード例 #5
0
                #pragma warning disable 108
        internal void Connect()
        {
            if (stompConnectionState != StompConnectionState.NotConnected)
            {
                return;
            }

            stompConnectionState = StompConnectionState.Opening;
            Debug.Log(TAG + "Opening Web Socket...");
            try {
                base.Connect();
            } catch (Exception) {
                stompConnectionState = StompConnectionState.NotConnected;
                // TODO: 8/10/16 AD Inform about fail
            }
        }
コード例 #6
0
        internal StompWebSocket(String url, Dictionary <String, String> extraHeaders, StompWebSocketEventHandler eventHandler) : base(url)
        {
            this.eventHandler    = eventHandler;
            stompConnectionState = StompConnectionState.NotConnected;

            foreach (KeyValuePair <String, String> header in extraHeaders)
            {
                Debug.Log("==> " + header.Key + " " + header.Value);
                SetCookie(new Cookie(header.Key, header.Value));
            }

            OnClose   += new EventHandler <CloseEventArgs> (onClose);
            OnOpen    += new EventHandler(onOpen);
            OnMessage += new EventHandler <MessageEventArgs> (onMessage);
            OnError   += new EventHandler <ErrorEventArgs> (onError);
        }
コード例 #7
0
        /// <summary>
        /// Changes the state of the connection.
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        ValueTask SetState(StompConnectionState state, Exception exception = null, CancellationToken cancellationToken = default)
        {
            if (this.state == state)
            {
                return(ValueTaskHelper.CompletedTask);
            }

            // update state and let any listeners know
            this.state = state;
            if (StateChanged != null)
            {
                return(OnStateChanged(new StompConnectionStateChangeArgs(state, exception, cancellationToken)));
            }

            return(ValueTaskHelper.CompletedTask);
        }
コード例 #8
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;
        }
コード例 #9
0
        internal virtual void OnConnected(StompMessage stompMessage)
        {
            String username  = stompMessage.getHeaders() ["user-name"];
            String userId    = stompMessage.getHeaders() ["user-id"];
            String heartbeat = stompMessage.getHeaders() ["heart-beat"];

            if (heartbeat != null)
            {
                String[] splittedHeartbeat = heartbeat.Split(',');
                int?     heartbeatPeriod   = Int32.Parse(splittedHeartbeat [1]);
                if (heartbeatPeriod != null && heartbeatPeriod > 0)
                {
                    ((BacktoryStompWebSocket)this).SetHeartbeatPeriod((double)heartbeatPeriod);
                }
            }

            Debug.Log(TAG + "Web Socket connected to server");
            stompConnectionState = StompConnectionState.Connected;
            eventHandler.OnConnected(username, userId);
        }
コード例 #10
0
 private void onDisconnected()
 {
     stompConnectionState = StompConnectionState.Closing;
     base.Close();
 }
コード例 #11
0
 private void onClose(object sender, EventArgs e)
 {
     stompConnectionState = StompConnectionState.NotConnected;
     Debug.Log(TAG + "Web Socket closed");
     eventHandler.OnDisconnected();
 }
コード例 #12
0
 private void onOpen(object sender, EventArgs e)
 {
     stompConnectionState = StompConnectionState.Connecting;
     transmit(COMMAND_CONNECT, getExtraHeaders(), null);
     Debug.Log(TAG + "...Web Socket Opened");
 }
コード例 #13
0
 private void OnStateChanged(StompConnectionState state)
 {
     _logger.Debug($"Connection state: {state}");
     StateChanged?.Invoke(this, new StompConnectionStateChangedEventArgs(state));
 }
コード例 #14
0
 void ITransportEventHandler.OnDisconnected()
 {
     State = StompConnectionState.Disconnected;
 }
コード例 #15
0
 public StompConnectionStateChangedEventArgs(StompConnectionState state)
 {
     State = state;
 }
コード例 #16
0
 /// <summary>
 /// Initializes a new instance.
 /// </summary>
 /// <param name="state"></param>
 /// <param name="exception"></param>
 public StompConnectionStateChangeArgs(StompConnectionState state, Exception exception, CancellationToken cancellationToken)
 {
     State     = state;
     Exception = exception;
 }