Giter Club home page Giter Club logo

Comments (4)

vtortola avatar vtortola commented on July 17, 2024

Hi!
You are copying your MemoryStream to a WebSocketWriteStream using Stream.CopyToAsync. If you want to notify progress, you can do the transfer between streams yourself and notify every X bytes.

For example you can create an extension method that copies from one stream to other, and calls some delegate each time you have copied at least the buffer length. This would be a close example of the idea in pseudo-C# (I do not have VS at hand at the moment, so it is from the top of my head):

public static async Task CopyToWithNotificationAsync(this Stream stream, Stream target, int bufferSize, Action notify)
{
    var buffer = new Byte[bufferSize];
    var readed = -1, completed = 0;
    while(readed != 0)
    {
         readed = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); 
         await target.WriteAsync(buffer, 0, readed).ConfigureAwait(false);
         completed += readed;
         if(completed >= bufferSize)
         { 
             notify();
             completed = 0;
         }
    }
}

Cheers.

from websocketlistener.

andrewmd5 avatar andrewmd5 commented on July 17, 2024

Reopening to ask a follow up question and again maybe I'm using the library improperly, but when using the following code.

 using (var messageWriter = socket.CreateMessageWriter(WebSocketMessageType.Binary))
                {
                    using (var stream = new MemoryStream(data))
                    {
                        await CopyToProgress(stream, messageWriter, 1000000); 

                    }
                }

Until the stream is done being copied/sent, no matter what thread the code is on, the remote endpoint can no longer receive or send any other messages until the binary message has completed. (Another client will still function fine) Is this intended behavior? I just find it odd OnMessage doesn't fire on the server or client for any other messages on the connected socket while the copy is in progress.

from websocketlistener.

vtortola avatar vtortola commented on July 17, 2024

Not sure I understood your question well...

When you are sending a Stream, it will be sent as a WebSocket message, although it will be sent in multiple frames. By the specification, control frames can be interpolated between message frames, but you cannot interpolate another message, therefore while you are sending a message to that client, you cannot send anything else. Now, if in your client you receive complete messages as a String or Byte[], probably the received event or whatever it has will trigger when the last message frame has been received. However in WSL when you are receiving you get a WebSocketReadMessageStream as soon as the first frame arrives from client.

That said, the client should be able of sending anything while is receiving. Make sure that when using WSL you are not blocking the read thread while sending, they should be two independent ones. If you are using the event listener please note that is a proof of concept and I would never use it in production :)

from websocketlistener.

andrewmd5 avatar andrewmd5 commented on July 17, 2024

Thanks for clearing all that up, answers my question perfectly.

from websocketlistener.

Related Issues (20)

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.