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; } }
#pragma warning restore 108 public virtual void Disconnect() { if (stompConnectionState != StompConnectionState.Connected) { return; } stompConnectionState = StompConnectionState.Disconnecting; transmit(COMMAND_DISCONNECT, null, null); }
protected virtual void Dispose(Boolean disposing) { if (State != Open) { throw new InvalidOperationException(CONNECTION_STATE); } socket.Close(); State = Closed; ((IDisposable)socket).Dispose(); }
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 }
#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 } }
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); }
/// <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); }
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; }
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); }
private void onDisconnected() { stompConnectionState = StompConnectionState.Closing; base.Close(); }
private void onClose(object sender, EventArgs e) { stompConnectionState = StompConnectionState.NotConnected; Debug.Log(TAG + "Web Socket closed"); eventHandler.OnDisconnected(); }
private void onOpen(object sender, EventArgs e) { stompConnectionState = StompConnectionState.Connecting; transmit(COMMAND_CONNECT, getExtraHeaders(), null); Debug.Log(TAG + "...Web Socket Opened"); }
private void OnStateChanged(StompConnectionState state) { _logger.Debug($"Connection state: {state}"); StateChanged?.Invoke(this, new StompConnectionStateChangedEventArgs(state)); }
void ITransportEventHandler.OnDisconnected() { State = StompConnectionState.Disconnected; }
public StompConnectionStateChangedEventArgs(StompConnectionState state) { State = state; }
/// <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; }