Merge pull request #8 from hardcodet/multiline-json
Support for continuous multi-line JSON objects
This commit is contained in:
@@ -26,8 +26,7 @@
|
||||
"drop": "true"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
],
|
||||
"Outputs": {
|
||||
"Redis": [
|
||||
{
|
||||
@@ -40,3 +39,4 @@
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NLog;
|
||||
|
||||
@@ -92,27 +90,22 @@ namespace TimberWinR.Inputs
|
||||
private void HandleNewClient(object client)
|
||||
{
|
||||
var tcpClient = (TcpClient)client;
|
||||
NetworkStream clientStream = null;
|
||||
|
||||
try
|
||||
{
|
||||
clientStream = tcpClient.GetStream();
|
||||
var stream = new StreamReader(clientStream);
|
||||
string line;
|
||||
while ((line = stream.ReadLine()) != null)
|
||||
NetworkStream clientStream = tcpClient.GetStream();
|
||||
using (var stream = new StreamReader(clientStream))
|
||||
{
|
||||
try
|
||||
//assume a continuous stream of JSON objects
|
||||
using (var reader = new JsonTextReader(stream) { SupportMultipleContent = true })
|
||||
{
|
||||
JObject json = JObject.Parse(line);
|
||||
while (reader.Read())
|
||||
{
|
||||
if (CancelToken.IsCancellationRequested) break;
|
||||
JObject json = JObject.Load(reader);
|
||||
ProcessJson(json);
|
||||
_receivedMessages++;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogManager.GetCurrentClassLogger().Error(ex);
|
||||
}
|
||||
if (CancelToken.IsCancellationRequested)
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -120,9 +113,6 @@ namespace TimberWinR.Inputs
|
||||
LogManager.GetCurrentClassLogger().Error(ex);
|
||||
}
|
||||
|
||||
if (clientStream != null)
|
||||
clientStream.Close();
|
||||
|
||||
tcpClient.Close();
|
||||
Finished();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user