alec1o / Netly

Cross-Platform and Multi-Protocol C# Socket Library. (Extremely fast and easy) 🇳 🇪 🇹 🇱 🇾
https://netly.docs.kezero.com
MIT License
58 stars 8 forks source link

Sample WebSocketClient & SampleWebSocketServer #43

Closed devwithu closed 2 months ago

devwithu commented 2 months ago

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();
            }
        }
    }
}
devwithu commented 2 months ago

question. i read tcp/ssl tls(https://netly.docs.kezero.com/#/), but it is confusing and old. Netly websocket support ssl ? i can use nginx proxy, but if Netly websocket support ssl then i will more happy. thanks.

alec1o commented 2 months ago

Netly HTTP.WebSocket (client) support https encrypted connection but HTTP.Server (websocket) server don't allow https connection, it's because is based with dotnet Http listener (don't allow https connection)

Read more about in #https://github.com/alec1o/Netly/discussions/36

devwithu commented 2 months ago

thanks answer. I should have read the document more carefully.

alec1o commented 2 months ago

I'll add websocket examples on README. Ihad forgotten to update the readme...

I had not put because all those still in development stage, and current branch is dev. Netly 3.x.x doesn't allow websocket connection.

Do you have a solution on how to implement SSL/TLS in HttpListener? :1st_place_medal:

I'm feel that version 4 will not be released and that this lib will die in development of this version. 💔
The only reason I haven't given up yet is because I need this lib version 4 to be able to create xLift a Gamelift and Agones alternative

Screenshot from 2024-04-23 15-09-49 Screenshot from 2024-04-23 15-10-40

devwithu commented 2 months ago

sorry. i do not have a solution on how to implement SSL/TLS in HttpListener.

I'm feel that version 4 will not be released and that this lib will die in development of this version.

sad news..

xLift is also cool. when i have a time, i will study xLift. and .net core websocket with ssl.

you can close this issue.

you are my hero. thanks you very much for netly, xLift and your kind replay.

alec1o commented 2 months ago

Thank you a lot, you made me motivated 🤱🏽 like my mother 🥹

Now I'll release this version in May or June.

About websocket client and http client the good news is both clients support and connect to encrypted server (wss;https)

  • But we need use Proxy like Nginx on server side to enable encrypted server
alec1o commented 2 months ago
Commit: f83e125dd4c6fb44d20d29a69e40fa4956e9a022
Readme preview: https://github.com/alec1o/Netly/tree/f83e125dd4c6fb44d20d29a69e40fa4956e9a022
Note:

I decided to add an example of how the library will be used after the release of version 4, the example contains examples of TCP, UDP, RUDP, WebSocket. Please note that some usability styles have also changed and do not match the current version.


I'm sorry, I had to change your example a little to be compatible with the launch version. And I also left this topic fixed as an example going deeper into the websocket readme,

The changes made to your code were:

  1. Add warning note about bufferType.
  2. Make the code compatible with release version.