GithubHelp home page GithubHelp logo

alec1o / netly Goto Github PK

View Code? Open in Web Editor NEW
53.0 4.0 8.0 2.67 MB

Socket library in C# Multi-Platform and Multi-Protocol. Extremely fast and easy. πŸ‡³ πŸ‡ͺ πŸ‡Ή πŸ‡± πŸ‡Ύ

Home Page: https://netly.docs.kezero.com

License: MIT License

C# 100.00%
http tcp websocket udp c-sharp dotnet ssl tls rudp mono network networking sockets realtime socket chat multiplayer

netly's Introduction

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

⭐ Your star is the light at the end of our tunnel.
Lead us out of the darkness by starring Netly on GitHub.
Star me please, I beg you! πŸ’™

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.Modify((socket) =>
    {
        printf("modify client socket e.g Enable NoDelay");
    });

    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);

// broadcast raw data for all connected client
server.To.DataBroadcast("text buffer");
server.To.DataBroadcast(new byte[] { 1, 2, 3 });

// broadcast event (netly event) for all connected client
server.To.EventBroadcast("event name", "text buffer");
server.To.EventBroadcast("event name", new byte[] { 1, 2, 3 });
UDP
πŸ“„ Client
using Netly;

UDP.Client client = new UDP.Client();
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();
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();

// broadcast raw data for all connected client
server.To.DataBroadcast("text buffer");
server.To.DataBroadcast(new byte[] { 1, 2, 3 });

// broadcast event (netly event) for all connected client
server.To.EventBroadcast("event name", "text buffer");
server.To.EventBroadcast("event name", new byte[] { 1, 2, 3 });
 
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
using Netly;

HTTP.Server server = new HTTP.Server();

// return true if server is serve http context
bool isServe = server.IsOpened;
server.On.Open(() =>
{
    // http server opened 
});
server.On.Close(() =>
{
    // http server closed 
});

server.On.Error((exception) =>
{
    // http server open error
});

server.On.Modify((httpListener) =>
{
    // HttpListener instance, called before try open connection.    
});

// Open http server connection
server.To.Open(new Uri("http://127.0.0.1:8080/"));

// Close http server connection
server.To.Close();
Map
// Map path
server.Map.Get("/", async (req, res) => {
    // Handle async: GET
})

server.Map.Post("/user", (req, res) => {
    // Handle sync: POST
});

// map using dynamic URL
server.Map.Delete("/post/{userId}/group/{groupId}", async (req, res)) => 
{
    string userId = req.Param["userId"];
    string groupId = req.Param["groupId"];
    
    // Handle async: Delete from dynamic URL path 
});

server.Map.WebSocket("/echo", (req, ws) =>
{
    // Handle websocket connection from path
});

/*
You can map: 
 * Get     # get request
 * Post    # post request
 * Delete  # delete request
 * Put     # put request
 * Patch   # patch request
 * Trace   # trace request
 * Options # options request
 * Head    # head request, (only head)
 * All     # all http nethod request
 * WebSocket   # websocket request
*/
    
Middleware
/* Note: Callback return
    true: next callback can be executed
    false: next or others callback can't executed

    Note: Middlewares is executed in added order
    Global middleware have more priority than Local middleware
*/

server.Middleware.Add(async (req, res) => {
    // e.g Verify user location
    
    // success, execute next callback
    return true;
});

// Global middleware
server.Middleware.Add(async (req, res) => {
    // e.g verify banned IP
    
    // success, execute next callback
    return true;
});

// Local middleware
server.Middleware.Add("/admin", async (req, res) => {
    // e.g Detect if the ip is allowed to access
     
    if(Foo.Bar(req) == false)
    {
        if (Foo.Bar() == true)
        {
            await res.Redirect("https://www.example.com");
        }
        else
        {
            res.Header.Add("Content-Type", "application/json;charset=UTF-8");
            await res.Send(404, "Response Content Here");   
        }            
    }
          
    // fail, stop and close connection if is opened
    return false;
});

// Local Middleware
server.Middleware.Add("/dashboard", async (req, res) =>
{
    // e.g Check session from cookies.
    return Foo.Bar(req.Cookies);
});
RUDP
πŸ“„ Client
using Netly;

RUDP.Client client = new RUDP.Client();
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, type) =>
{
    printf("connection received a raw data");
});

client.On.Event((name, bytes, type) =>
{
    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 }, RUDP.Unreliable);
client.To.Data("hello world", NE.Encoding.UTF8, RUDP.Reliable);

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

RUDP.Server server = new RUDP.Server();
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, type) =>
    {
        if (type == RUDP.Reliable) { ... }
        else if (type == RUDP.Unreliable) { ... }
        else { /* NOTE: it's imposible */ }
        
        printf("client received a raw data");
    });
    
    client.On.Event((name, type) =>
    
        if (type == RUDP.Reliable) { ... }
        else if (type == RUDP.Unreliable) { ... }
        else { /* NOTE: it's imposible */ }
        
        printf("client received a event");
    });    
});
// open connection
server.To.Open(new Host("127.0.0.1", 8080));

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

// broadcast raw data for all connected client
server.To.DataBroadcast("text buffer", RUDP.Unreliable);
server.To.DataBroadcast(new byte[] { 1, 2, 3 }, RUDP.Reliable);

// broadcast event (netly event) for all connected client
server.To.EventBroadcast("event name", "text buffer", RUDP.Unreliable);
server.To.EventBroadcast("event name", new byte[] { 1, 2, 3 }, RUDP.Reliable);
WebSocket
πŸ“„ Client
using Netly;

HTTP.WebSocket client = new HTTP.WebSocket();
client.On.Open(() =>
{
    // websocket connection opened
});

client.On.Close(() =>
{
    // websocket connection closed
});

client.On.Error((exception) =>
{
    // error on open websocket connectin
});

client.On.Data((bytes, type) =>
{
    if (type == HTTP.Binary) { ... }
    else if (type == HTTP.Text) { ... }
    else { /* NOTE: it's imposible */ }
    
    // raw data received from server
});

client.On.Event((name, bytes, type) =>
{
    if (type == HTTP.Binary) { ... }
    else if (type == HTTP.Text) { ... }
    else { /* NOTE: it's imposible */ }
    
    // event received from server
});

client.On.Modify((wsSocket) =>
{
    // modify websocket socket
});
// open websocket client connection
client.To.Open(new Uri("ws://127.0.0.1:8080/echo"));

// close websocket client connection
client.To.Close();

// send raw data for server
//      text message
client.To.Data("my message", HTTP.Text);
//      binnary message
client.To.Data(NE.GetBytes("my buffer"), HTTP.Binary);

// send event (netly event) for server
//      text message
client.To.Event("event name", "my message", HTTP.Text);
//      binnary message
client.To.Data("event name", NE.GetBytes("my buffer"), HTTP.Binary); 
πŸ“„ Server
using Netly;
using Netly.Interfaces;

HTTP.Server server = new HTTP.Server();

IHTTP.WebSocket[] Clients = server.WebSocketClients;
server.Map.WebSocket("/chat/{token}", async (req, ws) =>
{
    // Accept websocket from dynamic path
    string token = req.Params["token"];
    
    // validate websocket connection from params
    if (Foo.Bar(token) == false)
    {
        ws.To.Close();
    }
    
    ws.On.Modify(...);
    ws.On.Open(...);
    ws.On.Close(...);
    ws.On.Data(...);
    ws.On.Event(...);
});


server.Map.Websocket("/echo", (req, ws) =>
{
    // Handle websocket on /echo path
    
    ws.On.Modify((wsSocket) =>
    {
        // modify server-side websocket ocket
    });
    
    ws.On.Open(() =>
    {
        // server-side websocket connection opened
    });
    
    ws.On.Close(() =>
    {
        // server-side websocket connection closed
    });
    
    ws.On.Data((bytes, type) =>
    {
        if (type == HTTP.Binary) { ... }
        else if (type == HTTP.Text) { ... }
        else { /* NOTE: it's imposible */ }
        
        // server-side websocket received raw data
    });
    
    ws.On.Event((name, bytes, type) =>
    {
        if (type == HTTP.Binary) { ... }
        else if (type == HTTP.Text) { ... }
        else { /* NOTE: it's imposible */ }
        
        // server-side websocket received event
    });
});
server.On.Open(() =>
{
    // http server opened 
});
server.On.Close(() =>
{
    // http server closed 
});

server.On.Error((exception) =>
{
    // http server open error
});

server.On.Modify((httpListener) =>
{
    // HttpListener instance, called before try open connection.    
});

// Open http server connection
server.To.Open(new Uri("http://127.0.0.1:8080/"));

// Close http server connection
server.To.Close();
// open websocket client connection
server.To.Open(new Uri("ws://127.0.0.1:8080/echo"));

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

// broadcast raw data for all connected websocket socket
//      text message
server.To.WebsocketDataBroadcast("my message", HTTP.Text);
//      binnary message
server.To.WebsocketDataBroadcast(NE.GetBytes("my buffer"), HTTP.Binary);

// broadcast event (netly event) for all connected websocket socket
//      text message
server.To.WebsocketEventBroadcast("event name", "my message", HTTP.Text);
//      binnary message
server.To.WebsocketEventBroadcast("event name", NE.GetBytes("my buffer"), HTTP.Binary); 

Community examples From issues


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 mean 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]    
}

netly's People

Contributors

alec1o avatar dependabot[bot] avatar gulige avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

netly's Issues

Data framing in TCP at the socket level and not at the application level.

πŸ‘ https://stackoverflow.com/questions/73237486/creating-tcp-connection-and-getting-the-header

Protocols like http detect the framing of your data through the stream, and create the framing at the application level, Netly's function is to provide the socket and optionally contains the framing at the application level since version 3, much more I think netly it should be smart enough to create frames through the headers received by the sender. And that:
Relying on TCP headers to create a "frame" rather than creating a frame based on the data received.

Advantage of application-level framing?

  1. Easy to maintain.
    Disadvantage of application-level framing?
  2. Private protocol (When creating your framing, you use its resources to customize it so that it works perfectly in applications with the same protocol. The problem is when you are trying to communicate with libraries that have not been customized to use your framing protocol )
  3. Private in the bubble/Non-resilient (creating a protocol is easy, but it can be difficult to create clients and servers to keep them working with other clients)
  4. Overhead (you are adding more data than you actually need to receive)

Advantage of framing at socket level?

  1. Resilient (if you can put framing at the socket level, it means all applications communicate without dependency and overhead)

  2. No overhead (This is by sending both and receiving data the libraries do not need to send extra data in the message body to handle framing at the application level.

What am I trying to say with framing?

TCP has a very low data limit and if you want to send 1 megabyte you cannot send it linearly, that is, you need to divide this data and send it in small pieces and the framing function both at the application level and at the socket level is to detect and accumulate the data until it reaches 1 megabytes and only then release it to the receiving target. in case of download, it can be a file on the disk to write to the buffer each time it receives a fragment of data.
Netly by default will use the memory as a buffer, this works well if the data to be received is small, i.e. less than ~10 megabytes, and in case of receiving Gigabytes, Netly will need to use the gigabytes in RAM, and that's where the danger lies. and problem. Netly currently has framing at the application level and has a limit of 8 megabytes that it allows to receive and this can be customized up to as much GB, TB, etc. as you want. The next update to fix this should be to allow netly to write the buffer to disk in a temporary folder and then pass the file path instead of data in a parameter. This can be customized, for example from 10MB. In any case, this is a danger that resides in the framing of any layer,
But being realistic, most users don't even use 1MB, they use it to browse messages and small data and will rarely send large files. In any case, netly being able to deal with framing at the socket level brings compatibility without overhead and with data resilience.

Sockets problem on Android build

After many test i find it.

Runnig on Android 11 (same error) Real Device
Runnig on Android 9 (Android Emulator) MeMu Play
Runnig on Android 9 (Android Emulator) BlueStacks 5


The origin of error is internal and unknow

It's definitely a DOTNET issue on android. Netly uses System.Net.Sockets as the basis for socket creation. This library is having internal errors in the Android device.

My suspicions:

  1. Android Manifest needs some kind of permissions.
  2. The compilation of System.Net.Sockets internally is not working due to some internal type of conversion (C# to C++/Java or object).

Doubts:

  1. The problem is with the TCP server or TcpClient. What about the server that is not working (listening directly?) or the TcpClient that cannot connect to the socket? (Needs to be tested)

  2. In case the problem is the TcpServer that cannot listen to the localhost on Android or the TcpClient that cannot connect to the localhost. How should you solve it if the problem is internal? (Do nothing?)

  3. How to move forward? Continue writing socket using System.Net.*? or start the base in C++ and the interface in C#?

Originally posted by @alec1o in #33 (comment)

Hard found netly on nuget by search engines. e.g: ``google`` ``bing`` (v4)

Is your feature request related to a problem? Please describe.
Update Netly tags, because the package is hard found on nuget by topic search.

Describe the solution you'd like
add websokets and others relevant tags to be found easy on nuget.
e.g: netly socket sockets lib alec1o kezero ws websocket websockets websocket-server websocket-client client server ...

Describe alternatives you've considered
N/A

Additional context
Update tags on src/Netly.csproj
Update nuget readme to contain websocket and http examples.

Host obtain ip and by url using DNS

# current version
Host instance = new Host("127.0.0.1", 8080);
Host instance = new Host(...);
# new features required
Host instance = new Host("www.alec1o.com:8080");
Hacks
  • It will use DNS to found IP from url, and for prevent block program only will start find IP when Connect (Open) thread or call function like new Host("www.alec1o.com:7777").RunDNS()

Sample WebSocketClient & SampleWebSocketServer

I was looking for a library that supports both tcp and websocket in Unity.
I found it today and wrote code to test websocket.
It works well.
I hope this code is useful to someone who is new to netly.
Screenshot 2024-04-23 at 3 29 45β€―PM

I'll have to test it in Unity tomorrow.
thank you very much your effort.
Sample WebSocketClient & Sample WebSocketServer

NOTE: all received data are bytes, when bufferType is HTTP.Text it means the bytes are text converted to bytes (can be converted to text again), otherwise it means the bytes are Binary or HTTP.Binary and data cannot or should not be converted to text format

SampleWebSocketClient

using System.Text;
using Netly;

namespace SampleWebSocketClient;

class Program
{
    static void Main(string[] args)
    {
        var client = new HTTP.WebSocket();

        client.On.Open(() =>
        {
            Console.WriteLine("client.On.Open");
            
            // send message to server
            byte[] send = NE.GetBytes("hello world!", Encoding.UTF8);
            client.To.Data(send, HTTP.Binary);
        });

        client.On.Close(() =>
        {
            Console.WriteLine("client.On.Close");
        });

        client.On.Error((exception) =>
        {
            Console.WriteLine("client.On.Error " + exception.Message);
        });

        client.On.Data((bytes, bufferType) =>
        {
            string received = NE.GetString(bytes, Encoding.UTF8);
            Console.WriteLine(received);
        });

        client.On.Event((name, bytes, bufferType) =>
        {
            // websocket receives Netly event (Only Netly)
            // EXAMPLE:
            if (name == "client quit")
            {
                // send event to server
                client.To.Event("goodbye", "Some data here", bufferType);
                // close connection.
                client.To.Close();
            }
        });

        client.On.Modify((ws) =>
        {
            // modify socket
        });

        while (true)
        {
            if (!client.IsOpened)
            {
                client.To.Open(new Uri("ws://somedomain.com:8888/echo"));
                Console.WriteLine("client.To.Open");
                
                // Just Hold the Console
                Console.WriteLine("Message: ");
                string message = Console.ReadLine();
            }
            else
            {
                // Just Hold the Console
                Console.WriteLine("Message: ");
                string message = Console.ReadLine();
                    
            }
        }
    }
}

SampleWebSocketServer

using Netly;

namespace SampleWebSocketServer;

class Program
{
    static void Main(string[] args)
    {
        HTTP.Server server = new HTTP.Server();

        server.Map.WebSocket("/echo", (request, websocket) => {

            websocket.On.Data((bytes, bufferType) =>
            {
                // echo data.
                websocket.To.Data(bytes, bufferType);
            });

            websocket.On.Event((name, bytes, bufferType) =>
            {
                // echo event.
                websocket.To.Event(name, bytes, bufferType);
            });
        });


        server.On.Open(() =>
        {
            Console.WriteLine("server.On.Open");
        });

        server.On.Close(() =>
        {
            Console.WriteLine("server.On.Close");
        });

        server.On.Error((exception) =>
        {
            Console.WriteLine("server.On.Error" + exception.Message);
        });

        while (true)
        {
            if (!server.IsOpened)
            {
                server.To.Open(new Uri("http://somedomain.com:8888"));
            }
            else
            {
                // Just Hold the Console
                Console.WriteLine("Message: ");
                string message = Console.ReadLine();
            }
        }
    }
}

Thank you for your project bro

I used your project in a multiplayer game, and I am very grateful to you. Your project is excellent and very beginner-friendly.Looking forward to your game.
--爱ζ₯θ‡ͺ**

include API documentation and Guide (v4).

Is your feature request related to a problem? Please describe.

the docs of project is manual and maybe can have errors on api. and is hard mantain manual wrote, for fix that should consider include tools to generate api from sources using XML comments.

Describe the solution you'd like

A clear and concise description of what you want to happen.

Describe alternatives you've considered

  1. Found tools to generate docs from XML comments.
    1. DocFx is most easy way to do this! (Most better choice)

Additional context

If the choice is DocFx

    1. the guide must be rewrite. (current version don't have guide)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.