sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.73k stars 1.66k forks source link

Unable to handle large message size (>2Mb) #745

Open chaojian-zhang opened 9 months ago

chaojian-zhang commented 9 months ago

First of all, thanks for the great library! It works flawlessly for simple stuff and is a great starting point for getting feet wet with WebSockets or when I don't want to deal with all the nitty grittiness on my own. Besides what others say, I haven't run into performance problems so far - probably because I don't have that many client connections at the same time. On the other hand, the lack of proper net standard and .Net core support really makes this library less safe to use because Visual Studio is constantly warning about incompatibility issue.

I have seen quite a few other issues mentioning this issue (https://github.com/sta/websocket-sharp/issues/727, https://github.com/sta/websocket-sharp/issues/702, https://github.com/sta/websocket-sharp/issues/614, https://github.com/sta/websocket-sharp/issues/550, https://github.com/sta/websocket-sharp/issues/332, https://github.com/sta/websocket-sharp/issues/77), but I think I will start my own thread, as a mark for NOT using this library moving forward. At this moment, based on observations of issues above - there are barely any reply whatsoever - I think the original author of this library definitely do not have the capacity to maintain this library further or dealing with any of those mentioned issues. I advise future seekers either participate or contribute to this library, or maybe it's better to move elsewhere and implement their own library.

The key issue I am having right now is the library cannot handle large messages - try below for server and client:

using WebSocketSharp;
using WebSocketSharp.Server;

namespace Server
{
    public class MessageBehavior: WebSocketBehavior
    {
        protected override void OnMessage(MessageEventArgs e)
        {
            // Just echo
            Send(e.Data);
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            var server = new WebSocketServer("ws://localhost:9781");
            server.AddWebSocketService<MessageBehavior>("/Message");
            server.Start();
            Console.ReadKey(true);
            server.Stop();
        }
    }
}
using System.Text;
using WebSocketSharp;

namespace Client
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using var client = new WebSocket("ws://localhost:9781/Message");
            client.OnMessage += OnMessage;
            client.Connect();
            client.Send(GenerateReallyLongMessage());
            Console.ReadKey(true);
        }

        private static string GenerateReallyLongMessage()
        {
            StringBuilder buider = new();
            for (int i = 0; i < 10000; i++)
            {
                buider.Append($"{i}: ");
                for (int j = 0; j < 100; j++)
                    buider.Append($"{j},");
                buider.AppendLine();
            }
            return buider.ToString().TrimEnd();
        }

        private static void OnMessage(object? sender, MessageEventArgs e)
        {
            Console.WriteLine(e.Data);
        }
    }
}

It works well for 1000 but will not work for 10000 iterations, which is around 2Mb text data.

CosmicStud commented 9 months ago

It is more so a computer processing limitation. Entirely depends on the server/computer you use. Server level speeds max at about 2.5-10 mbs download/upload on reliable transmissions depending on the server. Unreliable can peak higher but it will cause a huge bottleneck on messages coming in/out... That is also with 20-60 dollar servers on Linode

If you use Unity, you can scale the devices max in/out speeds by the average FPS, for example a mobile device can do at max 1.5-3 mbs per second before bottlenecking