Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample WebSocketClient & SampleWebSocketServer #43

Open
devwithu opened this issue Apr 23, 2024 · 7 comments
Open

Sample WebSocketClient & SampleWebSocketServer #43

devwithu opened this issue Apr 23, 2024 · 7 comments
Labels
good first issue Good for newcomers Solved Question/Ask solved.

Comments

@devwithu
Copy link

devwithu commented Apr 23, 2024

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
Copy link
Author

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
Copy link
Owner

alec1o commented Apr 23, 2024

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 ##36

@devwithu
Copy link
Author

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

@alec1o alec1o added good first issue Good for newcomers Review Feedback and review! labels Apr 23, 2024
@alec1o alec1o added this to Netly Apr 23, 2024
@alec1o alec1o removed this from Netly Apr 23, 2024
@alec1o
Copy link
Owner

alec1o commented Apr 23, 2024

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? 🥇

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
Copy link
Author

devwithu commented Apr 24, 2024

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
Copy link
Owner

alec1o commented Apr 24, 2024

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
Copy link
Owner

alec1o commented Apr 24, 2024

Commit: f83e125
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.

@alec1o alec1o pinned this issue Apr 24, 2024
@alec1o alec1o closed this as completed Apr 25, 2024
@alec1o alec1o unpinned this issue Jun 7, 2024
@alec1o alec1o added Solved Question/Ask solved. and removed Review Feedback and review! labels Oct 7, 2024
@alec1o alec1o reopened this Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers Solved Question/Ask solved.
Projects
None yet
Development

No branches or pull requests

2 participants