Skip to content

A modular C# networking library for real-time communication. Supports plain WebSockets and Socket.IO with an event-driven system, JSON message handling, and extensible transport layers. Built for general applications requiring stable bidirectional networking.

License

Notifications You must be signed in to change notification settings

xsoftwareforge/NetworkLibrary

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NetworkLibrary

A modular C# networking library for real-time communication. Supports plain WebSockets and Socket.IO with an event-driven system, JSON message handling, and extensible transport layers. Built for general applications requiring stable bidirectional networking.

Features

  • Transport abstraction – plug in different realtime transports such as WebSockets or Socket.IO.
  • Event-driven API – register listeners for custom commands and receive callbacks when messages arrive.
  • JSON helpers – build and read messages using convenient builder and reader classes.
  • Mock server – optional in-memory server for local testing without a network connection.

Basic Usage

var wsManager = new WebSocketManager("wss://example.com", "logs");
var transport = new PlainWebSocketTransportAdapter(wsManager);
var client = new NetworkClient(transport);

await client.Connect();
await client.SendMessage("ping", new { message = "hello" });

Usage Examples

Plain WebSocket

Use the built-in WebSocketManager with the plain WebSocket adapter for basic connections.

var wsManager = new WebSocketManager("wss://echo.example", "logs");
var transport = new PlainWebSocketTransportAdapter(wsManager);
var client = new NetworkClient(transport);

client.AddListener("pong", payload => Console.WriteLine($"Received: {payload}"));

await client.Connect();
await client.SendMessage("ping", new { value = 1 });

Socket.IO

Connect to a Socket.IO server by instantiating SocketIoTransport. An optional function can provide an auth token when connecting.

var transport = new SocketIoTransport("https://socket.example",
    () => FetchAuthToken());

var client = new NetworkClient(transport);
await client.Connect();

await client.SendMessage("ping", new { message = "hello" });

JsonMessage Helpers

JsonMessage supplies builders and readers for working with strongly typed payloads.

var message = new JsonMessage.Builder()
    .WithCommand("update")
    .WithPayload(new JsonMessage.DataBuilder()
        .Add("x", 5)
        .Add("y", 6)
        .Build())
    .Build();

// Send the built message
await client.SendMessage(message.Command, message.Payload);

// Later, read values back
var reader = new JsonMessage.DataReader((Dictionary<string, object>)message.Payload);
int x = reader.GetInt("x");
int y = reader.GetInt("y");

License

This project is licensed under the MIT License.

About

A modular C# networking library for real-time communication. Supports plain WebSockets and Socket.IO with an event-driven system, JSON message handling, and extensible transport layers. Built for general applications requiring stable bidirectional networking.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages