Skip to content
/ Netly Public

Netly: Cross-Platform, Multi-Protocol C# Socket Library – Fast, Easy, and Versatile.⚡

License

Notifications You must be signed in to change notification settings

alec1o/Netly

 
 

Repository files navigation

powered by ALEC1O
netly logo
About

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


Documentation

Netly docs (netly.docs.kezero.com)


Install

Official publisher

Nuget Unity Asset Store
Install on Nuget Install on Asset Store

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.


Sponsor

Versions

Notable changes

v1 (old) v2 (old) v3 (stable) v4 (In development)
TCP client server TCP/IP Message Framing TLS/SSL client server WebSocket client server
UDP client server TCP/UDP performance increase UDP connection from (ping/timeout) HTTP client server
Message Framing performance increase TCP internal improvement
Message Framing new protocol Documentation improvement
Byter 2.0 XML description improvement
Collaborative documentation docsify

List of tested platforms

Feature

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

  • N/A

Dependency

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/

Demo
HTTP
    HTTP Client
    using System;
    using Netly;
    
    var client = new HttpClient();
    
    // error callback
    client.OnError((exception) =>
    {
        // request exception error. when connection doesn't open, null uri,...
    });
    
    // success callback
    client.OnSuccess((request) =>
    {
        // get status code
        int statusCode = request.StatusCode;
        // get server response as plain-text
        string bodyAsPlainText =  request.Body.PlainText;   
    });  
    
    // EXAMPLE:
        // set header
        client.Headers.Add("content-type", "multipart/form-data");
        // set url query
        client.Queries.Add("timeout", "1h");
          
        // create form data
        var body = new RequestBody(Enctype.Multipart);
        // set filename.
        body.Add("name", "Video.mp4");
        // set filebuffer.
        body.Add("file", new byte[]{ 1, 3, 4, 5, 6, 7, 8, 9, 0 });
        
        // set request body.
        client.Body = body;
    
    // Don't block main thread, run on threadpolls.
    // Send POST request.
    client.Send("POST", new Uri("https://drive.kezero.com/upload?timeout=1h"));
    HTTP Server
    using System;
    using Netly;
    
    var server = new HttpServer();
    
    server.OnOpen(() =>
    {
        //  server started.
    });
    
    server.OnClose(() =>
    {
        // server closed.
    });
    
    server.OnError((exception) =>
    {
        // error on open server connection.
    });
    
    server.MapAll("/foo", (request, response) =>
    {
        // receives request from all http methods.
        
        // EXAMPLE:
            // Send response for client.
            response.Send(200, $"Hello World. this http method is [{request.Method}]");
    });
    
    server.MapGet("/", (request, response) =>
    {
        // received GET request at "/" path.
    });
    
    server.MapPost("/login", (request, response) =>
    {
        // received POST request at "/login" path.
    
        // EXAMPLE:
            // request body on plain text.
            string text = request.Body.PlainText;  
            // get email from http form.
            string email = request.Body.Form.GetString("email");
            // get password from http from.
            string password = request.Body.Form.GetString("password");
            // get uploaded file from http form. (<form method="post" enctype="multipart/form-data">).
            byte[] picture = request.Body.Form.GetBytes("upload");  
    });
    
    server.Open(new Uri("https://localhost:8080"));

TCP
    Tcp Client
      using Netly;
      using Netly.Core;
      
      var client = new TcpClient(framing: true);
      
      // Enable SSL/TLS (onValidate delegate is optional)
      client.UseEncryption(enableEncryption: true, onValidate: null);
      
      client.OnOpen(() => 
      {
          // client connected
      });
      
      client.OnClose(() =>
      {
          // client disconnected
      });
      
      client.OnError((Exception exception) =>
      {
          // connection close because: 1.Error on connecting, 2.Invalid framing data
      });
      
      client.OnData((byte[] data) =>
      {
          // raw data received
      });
      
      client.OnEvent((string name, byte[] data) =>
      {
          // event received (event use netly protocol) 
      });
      
      client.OnModify((Socket socket) =>
      {
          // you can modify socket, called before open connection
      });
      
      client.Open(new Host("127.0.0.1", 8080));
    Tcp Server
    using Netly;
    using Netly.Core;
    
    var server = new TcpServer(framing: true);
    
    // Enable SSL/TLS
    byte[] pfxCert = <DO_SOMETHING>;
    string pfxPass = <DO_SOMETHING>;
    
    server.UseEncryption(pfxCert, pfxPass, SslProtocols.Tls13); // TLS v1.3
    
    server.OnOpen(() => 
    {
        // server start listen
    });
    
    server.OnClose(() =>
    {
        // server stop listen
    });
    
    server.OnError((Exception exception) =>
    {
        // error on start listen (connecting)
    });
    
    server.OnData((TcpClient client, byte[] data) =>
    {
        // a client receive raw data
    });
    
    server.OnEvent((TcpClient client, string name, byte[] data) =>
    {
        // a client receive event (event use netly protocol)
    });
    
    server.OnEnter((TcpClient client) =>
    {
        // a client connected on server
        
        client.OnClose(() =>
        {
            // alternative of: TcpServer.OnClose
        });
        
        client.OnData(() =>
        {
            // alternative of: TcpServer.OnData
        });
        
        client.OnEvent(() =>
        {
            // alternative of: TcpServer.OnEvent
        });
    });
    
    server.OnExit((TcpClient client) =>
    {
        // a client disconnected from server
    });
    
    server.OnModify((Socket socket) =>
    {
        // you can modify socket, called before listen and bind a port 
    });
    
    server.Open(new Host("127.0.0.1", 8080));

UDP
    Udp Client
      using Netly;
      using Netly.Core;
      
      var client = new UdpClient(useConnection: true, timeout: 10000 /* 10s */);
            
      client.OnOpen(() => 
      {
          // client connected
      });
      
      client.OnClose(() =>
      {
          // client disconnected
      });
      
      client.OnError((Exception exception) =>
      {
          // connection close because: 1.Error on connecting, 2.Invalid framing data
      });
      
      client.OnData((byte[] data) =>
      {
          // raw data received
      });
      
      client.OnEvent((string name, byte[] data) =>
      {
          // event received (event use netly protocol) 
      });
      
      client.OnModify((Socket socket) =>
      {
          // you can modify socket, called before open connection
      });
      
      client.Open(new Host("127.0.0.1", 8080));
    Udp Server
    using Netly;
    using Netly.Core;
    
    var server = new UdpServer(useConnection: true, timeout: 15000 /* 15s */);
    
    server.OnOpen(() => 
    {
        // server start listen
    });
    
    server.OnClose(() =>
    {
        // server stop listen
    });
    
    server.OnError((Exception exception) =>
    {
        // error on start listen (connecting)
    });
    
    server.OnData((UdpClient client, byte[] data) =>
    {
        // a client receive raw data
    });
    
    server.OnEvent((UdpClient client, string name, byte[] data) =>
    {
        // a client receive event (event use netly protocol)
    });
    
    server.OnEnter((UdpClient client) =>
    {
        // a client connected on server
        
        client.OnClose(() =>
        {
            // alternative of: TcpServer.OnClose
        });
        
        client.OnData(() =>
        {
            // alternative of: TcpServer.OnData
        });
        
        client.OnEvent(() =>
        {
            // alternative of: TcpServer.OnEvent
        });
    });
    
    server.OnExit((UdpClient client) =>
    {
        // a client disconnected from server
    });
    
    server.OnModify((Socket socket) =>
    {
        // you can modify socket, called before listen and bind a port 
    });
    
    server.Open(new Host("127.0.0.1", 8080));

WebSocket
    Websocket Client
    using System;
    using Netly;
    using Netly.Core;
    
    var client = new WebsocketClient();
    
    client.OnOpen(() =>
    {
        // websocket client connected.
    });
    
    client.OnClose(() =>
    {
        // websocket client disconnected.
    });
    
    client.OnError((exception) =>
    {
        // error on connect to server.
    });
    
    client.OnData((bytes, bufferType) =>
    {
        // websocket client received some data.
        // EXAMPLE:
            // send text data to server.
            client.ToData("hello world!");
            // send bynary data to server.
            client.ToData(new byte[]{ 1, 2, 3 });
    });
    
    client.OnEvent((name, bytes, bufferType) =>
    {
        // websocket receives Netly event (Only Netly)
            // EXAMPLE:
            if (name == "client quit")
            {
                // send event to server
                client.ToEvent("goodbye", "Some data here");
                // close connection.
                client.Close();
            }
    });
    
    client.OnModify((ws) =>
    {
        // modify socket
    });
    
    // open connection.
    client.Open(new Uri("ws:https://localhost:3000/"));
    Websocket Server
    using System;
    using Netly;
    
    var server = new HttpServer();
    
    server.OnOpen(() =>
    {
        //  server started.
    });
    
    server.OnClose(() =>
    {
        // server closed.
    });
    
    server.OnError((exception) =>
    {
        // error on open server connection.
    });
    
    // create websocket echo server.
    server.MapWebsocket("/echo", (request, client) =>
    {
        client.OnData((bytes, bufferType) =>
        { 
            // echo data.
            client.ToData(bytes, bufferType);
        });
    
        client.OnEvent((name, bytes, bufferType) =>
        { 
            // echo event.
            client.ToEvent(name, bytes, bufferType);
        });
    });
    
    // websocket server run on "/chat" route.
    server.MapWebsocket("/chat", (request, client) =>
    {    
        // websocket client connected.
        
        // EXAMPLE:
            // send data to client.
            client.ToData("some data");
            // send event to client.
            client.ToEvent("hello_client", "some data");
    
        // receive data
        client.OnData((bytes, bufferType) =>
        {
            // websocket connected receive some data.
        });
    
        client.OnClose(() =>
        {
            // websocket client disconnected.
        });
    
        // receive event.
        client.OnEvent((name, bytes, bufferType) =>
        {
            // websocket connected receive some event.
        });
    });
    
    server.Open(new Uri("https://localhost:8080"));