GithubHelp home page GithubHelp logo

Comments (6)

eidheim avatar eidheim commented on August 20, 2024

Consider this simpler example, send_stream1 and send_stream2 are here sent synchronously:

    //Example: Echo twice
    //  Sending received messages two times to connected client
    //  Test with the following JavaScript:
    //    var ws=new WebSocket("ws://localhost:8080/echo_twice");
    //    ws.onmessage=function(evt){console.log(evt.data);};
    //    ws.send("test");
    auto& echo_twice=server.endpoint["^/echo_twice/?$"];
    echo_thrice.onmessage=[&server](shared_ptr<WsServer::Connection> connection, shared_ptr<WsServer::Message> message) {
        auto message_str=message->string();

        auto send_stream1=make_shared<WsServer::SendStream>();
        *send_stream1 << message_str;
        //server.send is an asynchronous function
        server.send(connection, send_stream1, [&server, connection, message_str](const boost::system::error_code& ec) {
            if(!ec) {
                //send_stream1 has now been received by the client without error
                auto send_stream2=make_shared<WsServer::SendStream>();
                *send_stream2 << message_str;
                server.send(connection, send_stream2); //send_stream2 is now being sent
            }
        });
    };

edit: updated comments

from simple-websocket-server.

eidheim avatar eidheim commented on August 20, 2024

I would think, however, that creating some kind of synchronisation between the server and client would be a good idea. For instance:
Client: send me some data (for instance frame 0-4)
Server: here is data
Client: thank you, I need some more data now (for instance frame 5-9)
Server: here is some more data
Client: thank you, I need some more data now (for instance frame 10-14)
Server: here is some more data
and so on

Especially when creating a stream application, since you might want the client to buffer some of the stream, but only up to a point, and this would depend on data transferred and data shown on the client.

Edit: in the above case I would only use one server.send on the server side for each "Server: ....."

from simple-websocket-server.

alanywlee avatar alanywlee commented on August 20, 2024

Thanks for your instruction about synchronous data transfer. I doubt the delay I encountered is probably there are some limitation on size of binary data to send synchronously. Regarding to the simple modification shown in #23, I encountered more than 5 seconds delay even if I send whole binary file in one chunk.

from simple-websocket-server.

eidheim avatar eidheim commented on August 20, 2024

I saw now in #23 that you are using several client.send(send_stream, nullptr, 130);, that is with the same send_stream. This will lead to unexpected results, and is most likely causing the strange delay. Try to do client.send(send_stream, nullptr, 130); only once.

A new SendStream object needs to be created when doing server.send if one does not know if a previous server.send using a SendStream object has completed.

Edit: or better yet, create a new SendStream object for each server.send.

from simple-websocket-server.

alanywlee avatar alanywlee commented on August 20, 2024

Regarding to your instruction to send synchronous, it seems not able to send fix size (e.g., 80 pixels per chunk) of binary chunks using a for loop. Hard code 10 - 20 server.send(){} by hand seems a little bit wired.

from simple-websocket-server.

eidheim avatar eidheim commented on August 20, 2024

Avoid for loops when working with an asynchronous protocol like websocket. If you need to send several messages in one event, you should most likely implement it as a recursive function. Also you need to make sure your file stays open and is accessible in the callback (send_message2). I suggest though that you implement it as the Client-Message example I wrote in my second-last post. That is, with one server.send for each onmessage for instance. I think this would be simplest.

from simple-websocket-server.

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.