PurpleSofa 0.0.9

.NET 6.0
dotnet add package PurpleSofa --version 0.0.9
NuGet\Install-Package PurpleSofa -Version 0.0.9
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PurpleSofa" Version="0.0.9" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PurpleSofa --version 0.0.9
#r "nuget: PurpleSofa, 0.0.9"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install PurpleSofa as a Cake Addin
#addin nuget:?package=PurpleSofa&version=0.0.9

// Install PurpleSofa as a Cake Tool
#tool nuget:?package=PurpleSofa&version=0.0.9

PurpleSofa - C# .NET async tcp server & client

nuget .NET CI codecov License

feature

  • Callback for 'OnOpen'(accepted or connected), 'OnMessage'(received), 'OnClose'(received none).
  • Can store user value in session.
  • Check timeout at regular intervals by last receive time. It's useful to detect 'half close'.
  • 'OnClose' execution is taken via queue in order to avoid simultaneously many 'close'.

how to use

callback

public class Callback : PsCallback
{
    private const string Key = "inc";
    
    public override void OnOpen(PsSession session)
    {
        Console.WriteLine($"OnOpen {session}");
        session.SetValue(Key, 0);
        session.ChangeIdleMilliSeconds(5000);

        int inc = session.GetValue<int>(Key);
        session.Send($"inc: {inc}");
    }

    public override void OnMessage(PsSession session, byte[] message)
    {
        Console.WriteLine($"OnMessage {session} {Encoding.UTF8.GetString(message)}");
        int inc = session.GetValue<int>(Key);
        inc++;
        session.SetValue(Key, inc);
        session.Send($"inc: {inc}");
        if (inc > 3) session.Close();
    }

    public override void OnClose(PsSession session, PsCloseReason closeReason)
    {
        session.ClearValue(Key);
        int inc = session.GetValue<int>(Key);
        Console.WriteLine($"OnClose {session} {closeReason}, inc:{inc}");
    }
}

for server (ip v4)

public static void Main(string[] args)
{
    var server = new PsServer(new Callback());
    server.Start();
    server.WaitFor();
    // --- another thread
    // server.Shutdown();
}

for client (ip v4)

public static void Main(string[] args)
{
    var client = new PsClient(new Callback(), "127.0.0.1", 8710);
    client.Connect();
    // ...
    client.Disconnect();
}

for server (ip v6)

public static void Main(string[] args)
{
    var server = new PsServer(new Callback())
    {
        SocketAddressFamily = PsSocketAddressFamily.Ipv6
    };
    server.Start();
    server.WaitFor();
    // --- another thread
    // server.Shutdown();
}
  • Ipv4 socket is treated as ipv6 socket.
  • If host address 0.0.0.0, changed to ::.

for client (ip v6)

public static void Main(string[] args)
{
    var client = new PsClient(new Callback(), PsSocketAddressFamily.Ipv6, "::1", 8710);
    // Below is no problem
    // var client = new PsClient(new Callback(), "127.0.0.1", 8710);
    client.Connect();
    // ...
    client.Disconnect();
}
  • Ipv4 socket is treated as ipv6 socket.
  • If server is listening on ipv6, client is enable to connect to server like v4.
Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on PurpleSofa:

Package Downloads
FluentNetting

Fluent forwarded server.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.9 96 1/3/2023
0.0.8 107 12/20/2022
0.0.7 148 12/12/2022
0.0.6 380 1/24/2022
0.0.5 373 1/13/2022
0.0.4 165 12/21/2021
0.0.3 176 12/10/2021
0.0.2 201 12/2/2021
0.0.1 213 12/2/2021