Comments (6)
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.
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.
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.
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.
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.
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)
- [suggest] Send/Broadcast and Text/BinaryMode HOT 1
- Standalone Asio not found - but asio.hpp is present HOT 4
- re again WssClient::start causes on_error HOT 10
- When I use Google Chrome it happend a error. HOT 3
- exception thrown in server start (boost related) HOT 4
- Connection Timeout HOT 2
- Create example on how to stream data to a client HOT 1
- Unable to compile against standalone asio HOT 1
- How to stop the server? HOT 3
- Installed but ws_examples throws an error HOT 1
- Support for the Sec-WebSocket-Protocol field HOT 3
- fatal error C1083: Cannot open include file : 'pthread.h': No such file or directory HOT 1
- Messages split among frames aren't reasembled HOT 25
- Openssl wants void* instead of char* in 0x1000115fL HOT 1
- how to initiate send message from client HOT 3
- Client request error: End of file
- Access violation exeption HOT 3
- gdiplus
- Can't use wss client HOT 5
- Integration with Simple-Web-Server HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simple-websocket-server.