Lots of fixes, begin to think about timeouts

This commit is contained in:
Alex Hultman
2018-07-02 23:13:55 +02:00
parent 441d8bc4b4
commit a3dd3ef958
5 changed files with 67 additions and 19 deletions
+27 -15
View File
@@ -1,20 +1,21 @@
#include "uWS.h"
std::string buffer;
int connections = 0;
template<int SIZE, class T>
void respond(T *s) {
s->writeStatus("200 OK")->write([](int offset) {
return std::string_view(buffer.data() + offset, SIZE - offset);
}, SIZE);
}
//#define USE_SSL
int main(int argc, char **argv) {
// dynamically set respinse size from arguments
if (argc == 2) {
int bytes = atoi(argv[1]);
std::cout << "Server going to respond with " << bytes << " bytes of data" << std::endl;
buffer.resize(bytes);
} else {
std::cout << "Usage: uWS_test bytesInResponse" << std::endl;
return -1;
}
// 50 mb for huge
buffer.resize(52428800);
//uWS::init();
//uWS::Loop loop();
@@ -28,13 +29,20 @@ int main(int argc, char **argv) {
uWS::App app;
#endif
// todo: add timeouts and socket shutdown!
app.onGet("/", [](auto *s, auto *req, auto *args) {
// streams need to expose more information about lifetime!
s->writeStatus("200 OK")->write([](int offset) {
return std::string_view(buffer.data() + offset, buffer.length() - offset);
}, buffer.length());
s->writeStatus("200 OK")->writeHeader("Content-type", "text/html; charset=utf-8")->end("<h1>Welcome to µWebSockets v0.15!</h1>");
}).onGet("/tiny", [](auto *s, auto *req, auto *args) {
respond<512>(s);
}).onGet("/small", [](auto *s, auto *req, auto *args) {
respond<4096>(s);
}).onGet("/medium", [](auto *s, auto *req, auto *args) {
respond<16384>(s);
}).onGet("/large", [](auto *s, auto *req, auto *args) {
respond<51200>(s);
}).onGet("/huge", [](auto *s, auto *req, auto *args) {
respond<52428800>(s);
}).onPost("/upload", [](auto *s, auto *req, auto *args) {
s->read([s](std::string_view chunk) {
@@ -45,6 +53,10 @@ int main(int argc, char **argv) {
}).onWebSocket("/wsApi", []() {
}).onHttpConnection([](auto *s) {
std::cout << "Connections: " << ++connections << std::endl;
}).onHttpDisconnection([](auto *s) {
std::cout << "Connections: " << --connections << std::endl;
}).listen("localhost", 3000, 0);
uWS::run();