Navigation Menu

Skip to content

Netly is a flexible socket library built on C# that supports TCP, SSL, UDP, RUDP, HTTP and WebSocket protocols. It allows users to create and manage client-server connections with encryption and event handling options. It is compatible with various OS such as Android, iOS, Linux, macOS and Windows

License

alec1o/Netly

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

The active development branch is 'dev', while the production branch is 'main'.

Netly version 4 will be released soon, help validating the new way of interacting with netly. See more


powered by ALEC1O
Project

Get basic information about this project called Netly

Overview

Netly is a powerful C# socket library that simplifies network communication. It supports HTTP, TCP, SSL/TLS, UDP, Reliable UDP (RUDP) and WebSocket protocols, making it ideal for building multiplayer games, chat applications, and more.


Link's
Repository: github.com/alec1o/netly
Documentation: netly.docs.kezero.com


Install

Official publisher

Nuget Unity Asset Store
Install on Nuget Install on Asset Store

Sponsor
KeZero sponsor notice
JetBrains sponsor notice

Supporter
Why Contribute to Netly

Solve Real-World Challenges: Netly simplifies socket programming, making it accessible for developers. By contributing, you’ll directly impact how games, chat applications, and real-time systems communicate.

Learn and Grow: Dive into the world of networking, encryption, and protocols. Gain practical experience by working on a versatile library used across platforms.

Be Part of Something Bigger: Netly is open source, and your contributions will benefit the entire community. Join a passionate group of developers who believe in collaboration and knowledge sharing.

Code, Ideas, and Feedback: Whether you’re a seasoned developer or just starting out, your code, ideas, and feedback matter. Every line of code, every suggestion, and every bug report contributes to Netly’s growth.



Versions

Notable changes

4.x.x Development HTTP client and server support Reliable UDP (RUDP) client and server support WebSocket client and server Syntax and internal improvement Code XML comments improvement Documentation improvement by DocFx
3.x.x Stable TCP with TLS/SSL support UDP with connection (timeout response) New Message Framing protocol and performance increase Update for Byter 2.0 Docsify as documentation framework
2.x.x Legacy TCP with Message Framing support TCP and UDP performance increase
1.x.x Legacy TCP support UDP Support

Integrations

Technical descriptions about integrations

List of tested platforms
  • .NET (SDK)
  • Mono (SDK)
  • Unity (Engine)
  • Operating system (OS)
    • Linux
    • Windows
    • Android
    • iOS and macOS

    • Notice: This library might run on all devices. If it doesn't work on any device, it should be considered a bug and reported.

Dependencies

Build
Build dependencies
Build step-by-step
# 1. clone project
$ git clone "https://github.com/alec1o/Netly" netly 

# 2. build project
$ dotnet build "netly/" -c Release -o "netly/bin/"

# NOTE:
# Netly.dll require Byter.dll because is Netly dependency
# Netly.dll and Byter.dll have on build folder <netly-path>/bin/

Features

Below are some missing features that are planned to be added in later versions.

  • N/A


Examples

Code highlights

TCP
πŸ“„ Client
using Netly;

TCP.Client client = new TCP.Client(framing: true);
client.On.Open(() =>
{   
    printf("connection opened");
});

client.On.Close(() =>
{
    printf("connetion closed");
});

client.On.Error((exception) =>
{
    printf("connection erro on open");
});

client.On.Data((bytes) =>
{
    printf("connection receive a raw data");
});

client.On.Event((name, data) =>
{
    printf("connection receive a event");
});

client.On.Modify((socket) =>
{
    printf("called before try open connection.");
});

client.On.Encryption((certificate, chain, errors) =>
{
    // Only if client.IsEncrypted is enabled
    printf("validate ssl/tls certificate");
    // return true if certificate is valid
    return true;
});
// open connection if closed
client.To.Open(new Host("127.0.0.1", 8080));

// close connection if opened
client.To.Close();

// send raw data if connected
client.To.Data(new byte[2] { 128, 255 });
client.To.Data("hello world", NE.Encoding.UTF8);

// send event if connected
client.To.Event("name", new byte[2] { 128, 255 });
client.To.Event("name", "hello world", NE.Encoding.UTF8); 

// enable encryption (must call before client.To.Open)
client.To.Encryption(true); 
πŸ“„ Server
using Netly;

TCP.Server server = new TCP.Server(framing: true);
server.On.Open(() =>
{   
    printf("connection opened");
});

server.On.Close(() =>
{
    printf("connection closed");
});

server.On.Error((exception) =>
{
    printf("connection error on open");
});

server.On.Accept((client) =>
{
    client.On.Open(() =>
    {
        printf("client connected");
    });
    
    client.On.Data((bytes) =>
    {
        printf("client receive a raw data");
    });
    
    client.On.Event((name, bytes) =>
    {
        printf("client receive a event");
    });
    
    client.On.Close(() =>
    {
        printf("client disconnected");
    });
});

server.On.Modify((socket) =>
{
    printf("called before try open connection.");
});
// open connection
server.To.Open(new Host("1.1.1.1", 1111)); 

// close connection
server.To.Close();

// enable encryption support (must called before server.To.Open)
server.To.Encryption(enable: true, @mypfx, @mypfxpassword, SslProtocols.Tls12);
UDP
πŸ“„ Client
using Netly;

UDP.Client client = new UDP.Client(useConnection: true, timeout: 15000);
client.On.Open(() =>
{
    printf("connection opened");
});

client.On.Close(() =>
{
    printf("connection closed");
});

client.On.Error((exception) =>
{
    printf("connection error on open");
});

client.On.Data((bytes) =>
{
    printf("connection received a raw data");
});

client.On.Event((name, eventBytes) =>
{
    printf("connection received a event");
});

client.On.Modify((socket) =>
{
   printf("called before try open connection.");
});
// open connection if closed
client.To.Open(new Host("127.0.0.1", 8080));

// close connection if opened
client.To.Close();

// send raw data if connected
client.To.Data(new byte[2] { 128, 255 });
client.To.Data("hello world", NE.Encoding.UTF8);

// send event if connected
client.To.Event("name", new byte[2] { 128, 255 });
client.To.Event("name", "hello world", NE.Encoding.UTF8); 
πŸ“„ Server
using Netly;

UDP.Server server = new UDP.Server(useConnection: true, timeout: 15000);
server.On.Open(() =>
{
    printf("connection opened");
});

server.On.Close(() =>
{
    printf("connection closed");
});

server.On.Error((exception) =>
{
    printf("connection error on open");
});

server.On.Accept((client) =>
{
    client.On.Open(() =>
    {
        printf("client connected");
    });
    
    client.On.Close(() =>
    {
        // Only if use connection is enabled.
        printf("client disconnected");
    });
    
    client.On.Data((bytes) =>
    {
        printf("client received a raw data");
    });
    
    client.On.Event((name, bytes) =>
    {
        printf("client received a event");
    });
});
// open connection
server.To.Open(new Host("127.0.0.1", 8080));

// close connection
server.To.Close();
HTTP
πŸ“„ Client
using Netly;

HTTP.Client client = new HTTP.Client();

// add http header for request
client.Headers.Add("Content-Type", "json");
client.Headers.Add("Token", "ImGui.h");

// add http url queries e.g: https://www.alec1o.com/?page=about&version=4
client.Queries.Add("page", "about");
client.Queries.Add("version", "4");

// set request timeout (ms) default 15s (15000ms), 0 or negative value means infinite timeout.
client.Timeout = 6000; // 6s

// is opened: while is requesting
bool isFetching = client.IsOpened;
HttpClient http = null;

// called before try connect to server
// modify the HttpClient object
client.On.Modify((HttpClient instance) =>
{
    http = instance;
});

// connection is opened and fetch server.
client.On.Open((response) =>
{
    // you can use "http" instance on this scope (isn't null)
    if (http.<foo> == <bar>) { ... }
});

// erro on fetch, it can be timeout or whatever error
// but if you receives error it mean the operation is called or done
client.On.Error((Exception exception) =>
{
    Ny.Logger.PushError(exception);
});

// connection is closed with fetch server.
client.On.Close(() =>
{
     if (http.<bar> == <foo>) { ... }
});
// used to fetch a server
client.To.Open("method e.g GET", "url", "body, allow null");

// used for cancel opened request
client.To.Close();
πŸ“„ Server
RUDP
πŸ“„ Client
πŸ“„ Server
WebSocket
πŸ“„ Client
πŸ“„ Server

Usage

Integration and interaction example codes

Standard
πŸ“„ Console
using System;
using Netly;

public class Program
{
    private static void Main(string[] args)
    {
        UDP.Client client = new UDP.Client();
        
        client.On.Open(() =>
        {
            Console.WriteLine(<some-text-here>);
        };
            
        client.On.Close(() =>
        {
            Console.WriteLine(<some-text-here>);
        };
            
        client.On.Error((exception) =>
        {
            Console.WriteLine(<some-text-here>);
        };
        
        while(true)
        {
            if(!client.IsOpened)
            {
                client.To.Open(new Host("1.1.1.1", 1111));
            }
            else
            {
                Console.WriteLine("Message: ");
                string message = Console.ReadLine();
                client.To.Data(message ?? "No message.", NE.Encoding.UTF8);
            }
        }
    }
}
Flax Engine
πŸ“„ Script
using System;
using FlaxEngine;
using Netly;

public class Example : Script
{
    public string message;
    
    internal UDP.Client client;
    
    public override void Awake()
    {
        client = new UDP.Client();        
        
        client.On.Open(() =>
        {
            Debug.Log(<some-text-here>);
        };
            
        client.On.Close(() =>
        {
            Debug.Log(<some-text-here>);
        };
            
        client.On.Error((exception) =>
        {
            Debug.Log(<some-text-here>);
        };
    }
    
    public override void Start()
    {
        client.To.Open(new Host("1.1.1.1", 1111));
    }
    
    public override void Update()
    {
        if(!client.IsOpened)
        {
             client.To.Open(new Host("1.1.1.1", 1111));
        }
        else
        {            
            if (Input.GetKeyDown(KeyCode.Space))
            {
                client.To.Data(message ?? "No message.", NE.Encoding.UTF8);
            }
        }
    }
}
Unity Engine
πŸ“„ MonoBehaviour
using System;
using FlaxEngine;
using Netly;

public class Example : MonoBehaviour
{
    public string message;
    
    internal UDP.Client client;
    
    private void Awake()
    {
        client = new UDP.Client();        
        
        client.On.Open(() =>
        {
            Debug.Log(<some-text-here>);
        };
            
        client.On.Close(() =>
        {
            Debug.Log(<some-text-here>);
        };
            
        client.On.Error((exception) =>
        {
            Debug.Log(<some-text-here>);
        };
    }
    
    private void Start()
    {
        client.To.Open(new Host("1.1.1.1", 1111));
    }
    
    private void Update()
    {
        if(!client.IsOpened)
        {
             client.To.Open(new Host("1.1.1.1", 1111));
        }
        else
        {            
            if (Input.GetKeyDown(KeyCode.Space))
            {
                client.To.Data(message ?? "No message.", NE.Encoding.UTF8);
            }
        }
    }
}
WARNING: You should never initialize events in an uncontrolled loop, (**.On) stores functions that will be called when something happens and these functions only need to be initialized once. Understand, It doesn't means that every event will only have one callback attached to it, but it means not to keep calling (**.On) frequently like in Loops. See examples below of good and bad use.

For methods (**.To) there is an internal barrier that limits things like (trying to open or close connections several times, sending data with a disconnected socket, ...) although these methods do not cause problems when called in a loop, it is always good have the action and state in sync, for example only sending data when confirming that the connection is open.

πŸ“„ Code
using System;
using Netly;


private HTTP.WebSocket ws;
// OK
private void Init()
{    
    ws.On.Open(() => { ... });
    
    ws.On.Event((name, bytes) => { ... });

    ws.On.Event((name, bytes) =>
    {
        if (name == "foo") { ... }
    });

    ws.On.Event((name, bytes) =>
    {
        if (name == "bar") { ... }
    });
}
// BAD
public void Loop()
{    
    
    client.To.Open(...);    // [OK]
    
    client.To.Data(...);    // [OK]
    
    client.To.Event(...);   // [OK]
    
    client.To.Close(...);   // [OK]   
    
    
    ws.On.Open(() => { ... });       // [NEVER IN LOOP]
    
    ws.On.Close(() => { ... });      // [NEVER IN LOOP]
    
    ws.On.Data((bytes) => { ... });  // [NEVER IN LOOP]    
    
    ws.On.Error((exception) => { ... });    // [NEVER IN LOOP]
    
    ws.On.Event((name, bytes) => { ... });  // [NEVER IN LOOP]    
}

About

Netly is a flexible socket library built on C# that supports TCP, SSL, UDP, RUDP, HTTP and WebSocket protocols. It allows users to create and manage client-server connections with encryption and event handling options. It is compatible with various OS such as Android, iOS, Linux, macOS and Windows

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Languages