Comments (8)
I found a much easier way to handle this. It seems that requests in crow store the IO Service object, which is what you need to send data between the server and the client, meaning a method could be easily created to take advantage of that and send data without relying on the Connection object.
only potential hiccup would be SSL (as in OpenSSL) support, since an SSL connection also requires SSL context to function, so some form of that.
I believe most people don't use SSL so I'll try to create some example code that uses the IO Service to send data before the response is completed and will update this comment.
Update:
I edited some bits in the res-push
branch, this will not work with SSL, IDK if it'll compile even, it's just a patch to get the ability to send data in chunks.
Here's what I put in a simple CROW_ROUTE to allow chunked encoding, I'm not fully sure on how to use event-stream
res.set_header("Transfer-Encoding", "Chunked");
res.send_chunk(*req.adaptor, "6\r\nHello \r\n");
res.send_chunk(*req.adaptor, "5\r\nWorld\r\n");
res.send_chunk(*req.adaptor, "0\r\n\r\n");
There's no need for res.end()
because res.send_chunk()
already sets it.
Sorry it took me almost a day to get this done, There was a problem with sending strings because of C strings.
from crow.
If I understand correctly, an event-stream allows a server to push data to a client line by line, rather than in 1 body. This is currently not implemented in Crow (with the exception of static files). Furthermore it might require quite a few changes to how routes work, although such changes would be similar to how Transfer-Encoding: Chunked
would work.
from crow.
I have kept trying, but to no result:
I have extended "response":
- With a pointer to a class of mine ("pEventStream"), which has a function "bool getEvent(std::string &buf)". This function will ask for each new packet to be sent by streaming, until it returns "false", in which case the sending must be finished.
- "bool is_event_stream()", to identify this new type of sending.
- New functions:
template<typename Adaptor>
void do_stream_event(Adaptor& adaptor)
{
if (is_event_stream())
{
write_streamed_event(pEventStream, adaptor);
}
}
template<typename Adaptor>
void write_streamed_event(CEventStream * pEventStream, Adaptor& adaptor)
{
// TODO: Here I have tried sending loops, and everything I could think of...
}
I have extended "Connection":
void do_write_general()
{
// New case
if (res.is_event_stream())
{
...
res.do_stream_event(adaptor_);
...
}
...
I have tried a lot of things, without getting any results.
I don't think this kind of botched job will work, and the engine will have to be completely reworked.
from crow.
I tried passing a lambda instead of the CEventStream
object, but the Adaptor template made it quite complicated, I'm also looking into having a custom type of handler (similar to CROW_ROUTE.websocket()
)
from crow.
Thanks a lot! I'll try!
from crow.
I'm trying, but I'm having problems with the client reading the data from the server correctly.
It could be my code's fault, of course.
from crow.
I am having a similar issue with receiving data as it comes in e.g. File uploads
Maybe something that wraps the read_async/write_async events in the ioservice?
Regarding the SSL, I think when I played with it in the past, I am by far not an expert on ASIO, I had to wrap it such that the interface for ssl and non-ssl was type erased and unified. Most things only need to send/recv after the server starts up, I think.
from crow.
@beached I'm not sure this is the same issue @jsanchezfr was facing.. Could you provide more detail?
Crow already has a wrapper for that abstracts plaintext and SSL adaptors. The reason I didn't include the SSL part is because response
isn't a template class and I wasn't sure making it one (or making a template function inside it) was the best idea.
from crow.
Related Issues (20)
- No body in the response to a GET that precedes a HEAD in the same TCP connection HOT 1
- Docker "templates" not working.
- is crow synchronous only? HOT 5
- Fully parsed Request not available in catchall handler HOT 1
- Undefined reference to `AcceptEx` and `GetAcceptExSockaddrs` HOT 2
- Websocket with lots of data and slow clients causes memory issues HOT 2
- mustache features like parents, blocks and dynamic name does not seem to work
- Middleware is not called in case of HTTP upgrade
- CORS problem in 1.1.0 HOT 6
- 101 Switching Protocol in json data on the first request HOT 3
- [Bug?] Running websocket example doesn't work HOT 2
- Not found Crow websocket in Conan HOT 2
- Crash in chat example HOT 4
- Extend GitHub action to build with gcc and clang on the platforms
- Chat Example miss-leading
- Websocket Question HOT 4
- How can i know the progress of one post request? HOT 2
- Easily produced 'Worker Crash' with many simple concurrent http requests
- debian package is missing from the release section of version 1.1.0
- Please add new released version 1.1.0 to vcpkg HOT 1
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 crow.