public WebsocketJsonClient(Uri socketUrl, IWebsocketLogger logger = null, ICredentials credentials = null, int bufferSize = 1024 * 5, Encoding encoding = null, IWebProxy proxy = null) { _socketUrl = socketUrl; _logger = logger; _socketBuffer = new byte[bufferSize]; _encoding = encoding ?? Encoding.UTF8; if (credentials != null) { _socket.Options.Credentials = credentials; } if (proxy != null) { _socket.Options.Proxy = proxy; } if (_socket.Options.Credentials == null && !string.IsNullOrEmpty(_socketUrl.UserInfo) && _socketUrl.UserInfo.Contains(":")) { var split = _socketUrl.UserInfo.Split(':'); if (split.Length == 2) { _socket.Options.Credentials = new NetworkCredential(Uri.UnescapeDataString(split[0]), Uri.UnescapeDataString(split[1])); } } _jsonProcessor = new JsonProcessor(_logger); _jsonProcessor.OnEot += () => OnEot?.Invoke(); _jsonProcessor.OnEdges += (e) => OnEdges?.Invoke(e); _jsonProcessor.OnNode += (n) => OnNode?.Invoke(n); _jsonProcessor.OnStatus += (s) => OnStatus?.Invoke(s); }
public void ProcessMessage(string message) { if (message is null || message.Length < 3 || message[0] != '!') { message ??= "<null>"; if (message.StartsWith("Error", StringComparison.InvariantCultureIgnoreCase)) { _logger?.LogError(message); } else if (message.StartsWith("Warn", StringComparison.InvariantCultureIgnoreCase)) { _logger?.LogWarn(message); } else { _logger?.LogInfo(message); } return; } message = message.TrimStart('!'); try { var responseBase = Deserialize <CrawlerResponseBase>(message); switch (responseBase.Type) { case "eot": OnEot?.Invoke(); break; case "status": OnStatus?.Invoke(Deserialize <CrawlerResponseStatus>(message)); break; case "edges": OnEdges?.Invoke(Deserialize <CrawlerResponseEdges>(message)); break; case "node": OnNode?.Invoke(Deserialize <CrawlerResponseNode>(message)); break; default: _logger?.LogWarn($"Unknown Message Type `{responseBase.Type ?? "<null>"}`"); break; } } catch (JsonException ex) { _logger?.LogWarn($"invalid json: {message}", ex); } }