public Guid OnStarting() { Status = PluginStatus.Starting; ConnectedEvent.Reset(); return(Guid = Guid.NewGuid()); }
/// <inheritdoc /> public virtual Guid OnStarting() { Guid = Guid.NewGuid(); Status = PluginStatus.Starting; ConnectedEvent.Reset(); return(Guid); }
private async Task HandleConnection(CancellationTokenSource connectionCts) { Interlocked.Exchange(ref _hbLastAction, 0); using (_client = new ClientWebSocket()) { try { _ = Task.Run(() => HeartbeatWatcher(_client, connectionCts)); await _client.ConnectAsync(new Uri(_url), connectionCts.Token); ConnectedTime = DateTime.UtcNow; ConnectedEvent.Set(); ConnectedEvent.Reset(); Interlocked.Exchange(ref _hbLastAction, 0); var currentHello = HelloMessage; var helloAs = new ArraySegment <byte>(JsonSerializer.Serialize(currentHello)); await _client.SendAsync(helloAs, WebSocketMessageType.Text, true, connectionCts.Token); Interlocked.Exchange(ref _hbLastAction, 0); var bufferArray = new byte[ReceiveBufferSize]; while (_client.State == WebSocketState.Open && !connectionCts.IsCancellationRequested) { if (currentHello != HelloMessage) { currentHello = HelloMessage; helloAs = new ArraySegment <byte>(JsonSerializer.Serialize(currentHello)); await _client.SendAsync(helloAs, WebSocketMessageType.Text, true, connectionCts.Token); Interlocked.Exchange(ref _hbLastAction, 0); } var messageData = await WSUtils.ReceiveMessage(_client, connectionCts.Token, bufferArray); Interlocked.Exchange(ref _hbLastAction, 0); if (messageData.MessageType == WebSocketMessageType.Close) { return; } _queueThread.Enqueue(messageData); } } catch (TaskCanceledException) { await _client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal", CancellationToken.None); } catch (Exception ex) { OnError(ex); } } }
private async Task HandleConnection(CancellationTokenSource connectionCts) { _hbLastAction = DateTime.UtcNow; using (_client = new ClientWebSocket()) { try { _ = Task.Run(() => HeartbeatWatcher(_client, connectionCts)); await _client.ConnectAsync(new Uri(_url), connectionCts.Token); ConnectedEvent.Set(); ConnectedEvent.Reset(); _hbLastAction = DateTime.UtcNow; var currentHello = HelloMessage; var helloAs = new ArraySegment <byte>(JsonSerializer.Serialize(currentHello)); await _client.SendAsync(helloAs, WebSocketMessageType.Text, true, connectionCts.Token); _hbLastAction = DateTime.UtcNow; while (_client.State == WebSocketState.Open && !connectionCts.IsCancellationRequested) { if (currentHello != HelloMessage) { currentHello = HelloMessage; helloAs = new ArraySegment <byte>(JsonSerializer.Serialize(currentHello)); await _client.SendAsync(helloAs, WebSocketMessageType.Text, true, connectionCts.Token); _hbLastAction = DateTime.UtcNow; } var messageData = await WSUtils.ReceiveMessage(_client, connectionCts.Token); _hbLastAction = DateTime.UtcNow; if (messageData.MessageType == WebSocketMessageType.Close) { return; } _queueThread.Enqueue(messageData); } } catch (TaskCanceledException) { } catch (Exception ex) { OnError(ex); } } }