private async Task AcceptWebSocketCommands(WebSocketWrapper ws) { while (ws.WebSocket.IsConnected && !_cancel.IsCancellationRequested) { try { var message = await ws.WebSocket.ReadStringAsync(_cancel.Token).ConfigureAwait(false); if (message == null) // server shutting down { continue; } var json = JObject.Parse(message); var command = json.Property("command"); if (command == null || command.Value == null) { continue; } HandleCommand(command.Value.ToString(), json, ws); } catch { DisconnectWebSocket(ws.WebSocket); } } }
private void SendLogEntry(WebSocketWrapper ws, LogEntry logEntry) { try { if (!ws.WebSocket.IsConnected) { return; } if (ws.Expression != null && !ws.Expression.IsMatch(logEntry.Line)) { return; } using (var wsmsg = ws.WebSocket.CreateMessageWriter(WebSocketMessageType.Text)) using (var writer = new StreamWriter(wsmsg, Encoding.UTF8, 1024, true)) { _serializer.Serialize(writer, logEntry); } } catch { DisconnectWebSocket(ws.WebSocket); } }
private void HandleCommand(String commandName, JObject json, WebSocketWrapper wsWrapper) { try { foreach (var handler in _commandHandlers.Where(h => h.CanHandle(commandName))) { handler.Handle(json, wsWrapper); } } catch (Exception) { } }
private Boolean TryAddWebSocketToPool(WebSocket con) { try { _semaphore.EnterWriteLock(); if (_connections.Count >= _maxConnectedClients) { return(false); } var ws = new WebSocketWrapper(con); _connections.Add(ws); Task.Run(() => AcceptWebSocketCommands(ws)); return(true); } finally { _semaphore.ExitWriteLock(); } }
private Boolean TryAddWebSocketToPool(WebSocket con) { try { _semaphore.EnterWriteLock(); if (_connections.Count >= _maxConnectedClients) return false; var ws = new WebSocketWrapper(con); _connections.Add(ws); Task.Run(() => AcceptWebSocketCommands(ws)); return true; } finally { _semaphore.ExitWriteLock(); } }
private void HandleCommand(String commandName, JObject json, WebSocketWrapper wsWrapper) { try { foreach (var handler in _commandHandlers.Where(h=> h.CanHandle(commandName))) { handler.Handle(json, wsWrapper); } } catch(Exception) { } }
private async Task AcceptWebSocketCommands(WebSocketWrapper ws) { while (ws.WebSocket.IsConnected && !_cancel.IsCancellationRequested) { try { var message = await ws.WebSocket.ReadStringAsync(_cancel.Token).ConfigureAwait(false); if (message == null) // server shutting down continue; var json = JObject.Parse(message); var command = json.Property("command"); if (command == null || command.Value == null) continue; HandleCommand(command.Value.ToString(), json, ws); } catch { DisconnectWebSocket(ws.WebSocket); } } }
private void SendLogEntry(WebSocketWrapper ws, LogEntry logEntry) { try { if (!ws.WebSocket.IsConnected) return; if (ws.Expression != null && !ws.Expression.IsMatch(logEntry.Line)) return; using (var wsmsg = ws.WebSocket.CreateMessageWriter(WebSocketMessageType.Text)) using (var writer = new StreamWriter(wsmsg, Encoding.UTF8, 1024, true)) { _serializer.Serialize(writer, logEntry); } } catch { DisconnectWebSocket(ws.WebSocket); } }