Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

more than 5 seconds delay to send binary data #23

Closed
alanywlee opened this issue Nov 20, 2015 · 2 comments
Closed

more than 5 seconds delay to send binary data #23

alanywlee opened this issue Nov 20, 2015 · 2 comments

Comments

@alanywlee
Copy link

Based on ws_examples


auto send_stream=make_shared<WsClient::SendStream>();
{
    std::ifstream infile(JPG_FILE, ios::in | ios::binary);
    std::ofstream outfile ("new.jpg",std::ofstream::binary);

    // get size of file
    infile.seekg (0,infile.end);
    long nSize = infile.tellg();
    infile.seekg (0);

    char* buffer = new char[nSize];

    // read content of infile
    infile.read (buffer,nSize);

          for(int i=0; i<nSize; i+=SEND_SIZE)
          {
            auto send_stream=make_shared<WsClient::SendStream>();
            // write to outfile
            int send_size = i< (nSize-SEND_SIZE) ? SEND_SIZE:(nSize-i);
            cout << "Client: Sending binary: " << i << "," << send_size << endl;

            send_stream->write(&buffer[i], send_size);
            outfile.write (&buffer[i], send_size);
            client.send(send_stream, nullptr, 130);

            this_thread::sleep_for(chrono::milliseconds(200));
          }
    outfile.write (buffer, nSize);

    // release dynamically-allocated memory
    delete[] buffer;
    infile.close();
    outfile.close();
}
client.send(send_stream, nullptr, 130);

console output :


...
Client: Sending binary: 264000,8000
Client: Sending binary: 272000,8000
Client: Sending binary: 280000,8000
Client: Sending binary: 288000,8000
...


the same test case as issue #13
I sent a binary data using WsClient just like ws_example did.
I found even though the console shown data sent immediately, but websocket always got data after more than 5 seconds later. I used wireshock to confirm data from WsClient indeed arrived more than 5 seconds after client shown sent.

I also tried to use python websocket client to confirm delay is not because of network problem. Only
data sent by WsClient has long delay.

I am curious if I did anything wrong sort of caching the send buffer, but not really sent right after I called client.send()?

@eidheim
Copy link
Owner

eidheim commented Nov 20, 2015

The this_thread::sleep_for is causing the thread pool to become busy. If you want to send data synchronously you should do what is done in https://github.com/eidheim/Simple-WebSocket-Server/blob/master/ws_examples.cpp#L72 when sending send_stream1 and send_stream3 (after send_stream1 is finished sending).

@eidheim
Copy link
Owner

eidheim commented Nov 22, 2015

Closing this as it is not an issue in Simple-WebSocket-Server.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants