Skip to content

Commit

Permalink
Add SSE (server-sent event) supports.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcean committed Jul 29, 2024
1 parent e662670 commit db6e55f
Show file tree
Hide file tree
Showing 10 changed files with 741 additions and 117 deletions.
23 changes: 0 additions & 23 deletions Core/Collection/List/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,29 +1326,6 @@ public static IEnumerable<JsonObjectNode> WithProperty(this JsonArrayNode array,
public static IEnumerable<JsonObjectNode> WithProperty(this JsonArrayNode array, string key)
=> WithProperty(array?.SelectObjects(), key);

/// <summary>
/// Gets the JSON lines format string of the value.
/// </summary>
/// <param name="col">The input collection.</param>
/// <returns>A JSON lines format string.</returns>
public static string ToJsonlString(this IEnumerable<JsonObjectNode> col)
{
if (col == null) return null;
var str = new StringBuilder();
foreach (var prop in col)
{
if (prop is null)
{
str.AppendLine("null");
continue;
}

str.AppendLine(prop.ToString());
}

return str.ToString();
}

/// <summary>
/// Converts to JSON object node collection.
/// </summary>
Expand Down
88 changes: 88 additions & 0 deletions Core/Collection/List/StringsConverter.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,102 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Trivial.Net;
using Trivial.Text;

namespace Trivial.Collection;

public static partial class ListExtensions
{
/// <summary>
/// Gets the server-sent event format string.
/// </summary>
/// <param name="col">The input collection.</param>
/// <param name="newLineN">true if use \n instead of new line.</param>
/// <returns>A server-sent event format string.</returns>
public static string ToResponseString(this IEnumerable<ServerSentEventRecord> col, bool newLineN)
{
if (col == null) return null;
var sb = new StringBuilder();
foreach (var item in col)
{
if (item == null) continue;
item.ToResponseString(sb, newLineN);
}

return sb.ToString();
}

/// <summary>
/// Gets the server-sent event format string.
/// </summary>
/// <param name="col">The input collection.</param>
/// <param name="stream">The stream.</param>
/// <returns>A server-sent event format string.</returns>
public static void ToResponseString(this IEnumerable<ServerSentEventRecord> col, Stream stream)
{
if (col == null) return;
var writer = new StreamWriter(stream, Encoding.UTF8);
foreach (var item in col)
{
if (item == null) continue;
writer.Write(item.ToResponseString(true));
writer.Write('\n');
writer.Flush();
}
}

/// <summary>
/// Gets the JSON array format string of the value.
/// </summary>
/// <param name="col">The input collection.</param>
/// <returns>A JSON array format string.</returns>
public static string ToJsonArrayString(this IEnumerable<JsonObjectNode> col)
{
var arr = new JsonArrayNode();
arr.AddRange(col);
return arr.ToString();
}

/// <summary>
/// Gets the JSON array format string of the value.
/// </summary>
/// <param name="col">The input collection.</param>
/// <param name="indentStyle">The indent style.</param>
/// <returns>A JSON array format string.</returns>
public static string ToJsonArrayString(this IEnumerable<JsonObjectNode> col, IndentStyles indentStyle)
{
var arr = new JsonArrayNode();
arr.AddRange(col);
return arr.ToString(indentStyle);
}

/// <summary>
/// Gets the JSON lines format string of the value.
/// </summary>
/// <param name="col">The input collection.</param>
/// <returns>A JSON lines format string.</returns>
public static string ToJsonlString(this IEnumerable<JsonObjectNode> col)
{
if (col == null) return null;
var str = new StringBuilder();
foreach (var prop in col)
{
if (prop is null)
{
str.AppendLine("null");
continue;
}

str.AppendLine(prop.ToString());
}

return str.ToString();
}

/// <summary>
/// Generates a string collection.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions Core/IO/Stream/StreamCopy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,27 @@ public static IEnumerable<byte> ReadBytes(StreamPagingResolver streams, bool clo
public static IEnumerable<string> ReadLines(this TextReader reader, bool removeEmptyLine = false)
=> CharsReader.ReadLines(reader, removeEmptyLine);

/// <summary>
/// Reads lines from a specific stream reader.
/// </summary>
/// <param name="stream">The input stream.</param>
/// <param name="encoding">The stream encoding.</param>
/// <param name="removeEmptyLine">true if need remove the empty line; otherwise, false.</param>
/// <returns>Lines from the specific stream reader.</returns>
/// <exception cref="NotSupportedException">The stream does not support reading.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
/// <exception cref="ObjectDisposedException">The stream has disposed.</exception>
public static IEnumerable<string> ReadLines(this Stream stream, Encoding encoding = null, bool removeEmptyLine = false)
{
if (stream == null || !stream.CanRead) yield break;
using var reader = encoding == null ? new StreamReader(stream) : new StreamReader(stream, encoding);
var lines = ReadLines(reader, removeEmptyLine);
foreach (var line in lines)
{
yield return line;
}
}

/// <summary>
/// Converts a stream paging resolver to a stream collection.
/// </summary>
Expand Down
Loading

0 comments on commit db6e55f

Please sign in to comment.