mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-10 01:16:24 +00:00
wip
This commit is contained in:
parent
ad3959a8e9
commit
cf941fe5c9
@ -16,13 +16,21 @@ namespace CryptoExchange.Net.Converters
|
|||||||
public abstract Type? GetDeserializationType(Dictionary<string, string> idValues, List<MessageListener> listeners);
|
public abstract Type? GetDeserializationType(Dictionary<string, string> idValues, List<MessageListener> listeners);
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public object? ReadJson(Stream stream, List<MessageListener> listeners)
|
public ParsedMessage? ReadJson(Stream stream, List<MessageListener> listeners, bool outputOriginalData)
|
||||||
{
|
{
|
||||||
// Start reading the data
|
// Start reading the data
|
||||||
// Once we reach the properties that identify the message we save those in a dict
|
// Once we reach the properties that identify the message we save those in a dict
|
||||||
// Once all id properties have been read callback to see what the deserialization type should be
|
// Once all id properties have been read callback to see what the deserialization type should be
|
||||||
// Deserialize to the correct type
|
// Deserialize to the correct type
|
||||||
|
var result = new ParsedMessage();
|
||||||
|
|
||||||
using var sr = new StreamReader(stream, Encoding.UTF8, false, (int)stream.Length, true);
|
using var sr = new StreamReader(stream, Encoding.UTF8, false, (int)stream.Length, true);
|
||||||
|
if (outputOriginalData)
|
||||||
|
{
|
||||||
|
result.OriginalData = sr.ReadToEnd();
|
||||||
|
stream.Position = 0;
|
||||||
|
}
|
||||||
|
|
||||||
using var jsonTextReader = new JsonTextReader(sr);
|
using var jsonTextReader = new JsonTextReader(sr);
|
||||||
JToken token;
|
JToken token;
|
||||||
try
|
try
|
||||||
@ -34,6 +42,12 @@ namespace CryptoExchange.Net.Converters
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (token.Type == JTokenType.Array)
|
||||||
|
{
|
||||||
|
// Received array, take first item as reference
|
||||||
|
token = token.First!;
|
||||||
|
}
|
||||||
|
|
||||||
var typeIdDict = new Dictionary<string, string>();
|
var typeIdDict = new Dictionary<string, string>();
|
||||||
foreach(var idField in TypeIdFields)
|
foreach(var idField in TypeIdFields)
|
||||||
{
|
{
|
||||||
@ -42,9 +56,17 @@ namespace CryptoExchange.Net.Converters
|
|||||||
foreach (var splitToken in splitTokens)
|
foreach (var splitToken in splitTokens)
|
||||||
{
|
{
|
||||||
accessToken = accessToken[splitToken];
|
accessToken = accessToken[splitToken];
|
||||||
|
|
||||||
if (accessToken == null)
|
if (accessToken == null)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
if (accessToken.Type == JTokenType.Array)
|
||||||
|
{
|
||||||
|
// Received array, take first item as reference
|
||||||
|
accessToken = accessToken.First!;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
typeIdDict[idField] = accessToken?.ToString();
|
typeIdDict[idField] = accessToken?.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,19 +92,18 @@ namespace CryptoExchange.Net.Converters
|
|||||||
idString += item.Value;
|
idString += item.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.Identifier = idString;
|
||||||
var resultType = GetDeserializationType(typeIdDict, listeners);
|
var resultType = GetDeserializationType(typeIdDict, listeners);
|
||||||
return new ParsedMessage
|
result.Data = resultType == null ? null : token.ToObject(resultType);
|
||||||
{
|
|
||||||
Identifier = idString,
|
return result;
|
||||||
Data = resultType == null ? null : token.ToObject(resultType)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ParsedMessage
|
public class ParsedMessage
|
||||||
{
|
{
|
||||||
public string Identifier { get; set; } = null!;
|
public string Identifier { get; set; } = null!;
|
||||||
|
public string? OriginalData { get; set; }
|
||||||
public object? Data { get; set; }
|
public object? Data { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -582,6 +582,7 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
|
|
||||||
private async Task ProcessData(Stream stream, WebSocketMessageType messageType)
|
private async Task ProcessData(Stream stream, WebSocketMessageType messageType)
|
||||||
{
|
{
|
||||||
|
stream.Position = 0;
|
||||||
if (Parameters.Interceptor != null)
|
if (Parameters.Interceptor != null)
|
||||||
stream = Parameters.Interceptor.Invoke(stream);
|
stream = Parameters.Interceptor.Invoke(stream);
|
||||||
if (OnStreamMessage != null)
|
if (OnStreamMessage != null)
|
||||||
|
@ -329,7 +329,7 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
lock (_listenerLock)
|
lock (_listenerLock)
|
||||||
listeners = _messageListeners.OrderByDescending(x => x.Priority).ToList();
|
listeners = _messageListeners.OrderByDescending(x => x.Priority).ToList();
|
||||||
|
|
||||||
var result = (ParsedMessage)ApiClient.StreamConverter.ReadJson(stream, listeners.OfType<MessageListener>().ToList()); // TODO
|
var result = ApiClient.StreamConverter.ReadJson(stream, listeners.OfType<MessageListener>().ToList(), ApiClient.ApiOptions.OutputOriginalData ?? ApiClient.ClientOptions.OutputOriginalData); // TODO
|
||||||
if(result == null)
|
if(result == null)
|
||||||
{
|
{
|
||||||
stream.Position = 0;
|
stream.Position = 0;
|
||||||
@ -339,6 +339,9 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (result.OriginalData != null)
|
||||||
|
_logger.LogDebug($"Socket {SocketId} Data received: {result.OriginalData}");
|
||||||
|
|
||||||
if (result.Data == null)
|
if (result.Data == null)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Message not matched to type");
|
_logger.LogWarning("Message not matched to type");
|
||||||
@ -752,6 +755,8 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
// TODO check result?
|
// TODO check result?
|
||||||
return matches;
|
return matches;
|
||||||
}).ConfigureAwait(false);
|
}).ConfigureAwait(false);
|
||||||
|
|
||||||
|
_logger.Log(LogLevel.Information, $"Socket {SocketId} subscription {listener.Id} unsubscribed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user