public async Task WriteToWebSocket <TNotification>(TNotification notification) { _context.Reset(); _context.Renew(); _ms.SetLength(0); await using (var writer = new AsyncBlittableJsonTextWriter(_context, _ms)) { var notificationType = notification.GetType(); if (notificationType == typeof(DynamicJsonValue)) { _context.Write(writer, notification as DynamicJsonValue); } else if (notificationType == typeof(BlittableJsonReaderObject)) { _context.Write(writer, notification as BlittableJsonReaderObject); } else { ThrowNotSupportedType(notification); } } _ms.TryGetBuffer(out ArraySegment <byte> bytes); await _webSocket.SendAsync(bytes, WebSocketMessageType.Text, true, _resourceShutdown); }
private void CheckIfContextOrCacheNeedToBeRenewed() { if (_builderContext.AllocatedMemory > 4 * 1024 * 1024 || _docsCountOnCachedRenewSession > _maxDocsCountOnCachedRenewSession) { _builderContext.Reset(); _builderContext.Renew(); _docsCountOnCachedRenewSession = 0; return; } if (_builder.NeedClearPropertiesCache()) { _builderContext.CachedProperties.ClearRenew(); return; } ++_docsCountOnCachedRenewSession; }
public async Task StartSendingNotifications() { try { while (_cancellationTokenSource.IsCancellationRequested == false) { _context.Reset(); _context.Renew(); var result = await _manualResetEvent.WaitAsync(TimeSpan.FromMilliseconds(5000)).ConfigureAwait(false); if (IsAlive == false) { return; } if (result == false) { await SendMessage(WebSocketHelper.Heartbeat).ConfigureAwait(false); continue; } _manualResetEvent.Reset(); while (_messages.TryDequeue(out var message)) { _context.Reset(); _context.Renew(); if (IsAlive == false) { return; } await SendMessage(ToByteArraySegment(message)).ConfigureAwait(false); } } } catch (Exception e) { if (Logger.IsInfoEnabled) { Logger.Info("Error when handling web socket connection", e); } } finally { TrafficWatchManager.Disconnect(this); try { if (_disposed == false) { await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "NORMAL_CLOSE", _cancellationTokenSource.Token); } } catch { // ignore } } }