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); } }
public void Parse() { try { reader.MoveToContent(); while (reader.Read()) { var arg = new ParseEventArg { NodeType = reader.NodeType, Depth = reader.Depth, Name = reader.Name, EmptyElement = reader.IsEmptyElement }; if (reader.HasValue) { arg.Value = reader.Value; } else { arg.Value = null; } if (reader.HasAttributes) { var arr = new Attribute[reader.AttributeCount]; for (var i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); arr[i] = new Attribute { Name = reader.Name, Value = reader.Value, Index = i }; } reader.MoveToElement(); arg.Attributes = new ReadOnlyCollection <Attribute>(arr); } else { arg.Attributes = new ReadOnlyCollection <Attribute>(empty); } OnNode?.Invoke(this, arg); switch (reader.NodeType) { case XmlNodeType.Element: OnElement?.Invoke(this, arg); break; case XmlNodeType.CDATA: OnCData?.Invoke(this, arg); break; case XmlNodeType.Comment: OnComment?.Invoke(this, arg); break; case XmlNodeType.DocumentType: OnDocType?.Invoke(this, arg); break; case XmlNodeType.EndElement: OnEndElement?.Invoke(this, arg); break; case XmlNodeType.EntityReference: OnEntityReferenceUnresolved?.Invoke(this, arg); reader.ResolveEntity(); reader.Read(); arg.Name = reader.Name; arg.Value = reader.Value; arg.Depth = reader.Depth; arg.NodeType = reader.NodeType; OnEntityReference?.Invoke(this, arg); break; case XmlNodeType.ProcessingInstruction: OnProcessingInstruction?.Invoke(this, arg); break; case XmlNodeType.Text: OnText?.Invoke(this, arg); break; } } } catch (XmlException e) { OnException?.Invoke(this, e); } }