New interface a la designated initializers

This commit is contained in:
Alex Hultman
2018-11-22 00:20:38 +01:00
parent 519c1f5b60
commit fb70cf78e1
2 changed files with 27 additions and 23 deletions
+14 -18
View File
@@ -5,32 +5,28 @@
int main(int argc, char **argv) { int main(int argc, char **argv) {
// do websockets here // here we have it
uWS::App().get("/hello", [](auto *res, auto *req) {
/* res->end("Hello HTTP!");
uWS::App().ws("/*", { }).ws("/*", {
.open = []() { /*.open = */[](auto *ws, auto *req) {
}, },
.message = []() { /*.message = */[](auto *ws, std::string_view message, uWS::OpCode opCode) {
ws->send(message, opCode);
}
/*.drain = []() {
}, },
.drain = []() { .ping = []() {
},
.pong = []() {
}, },
.close = []() { .close = []() {
} }*/
}).listen().run();*/
uWS::App().get("/hello", [](auto *res, auto *req) {
res->end("Hello HTTP!");
}).ws("/*", [](auto *ws, auto *req) {
std::cout << "WebSocket conntected to URL: " << req->getUrl() << std::endl;
}, [](auto *ws, std::string_view message, uWS::OpCode opCode) {
ws->send(message, opCode);
}).listen(9001, [](auto *token) { }).listen(9001, [](auto *token) {
if (token) { if (token) {
std::cout << "Listening on port " << 3000 << std::endl; std::cout << "Listening on port " << 3000 << std::endl;
+13 -5
View File
@@ -28,6 +28,7 @@
#include "libwshandshake.hpp" #include "libwshandshake.hpp"
namespace uWS { namespace uWS {
template <bool SSL> template <bool SSL>
struct TemplatedApp { struct TemplatedApp {
private: private:
@@ -51,14 +52,19 @@ public:
// construct the websocket cintext? no! on demand! // construct the websocket cintext? no! on demand!
} }
// this method creates a new websocket context and attaches it to a path struct WebSocketBehavior {
TemplatedApp &ws(std::string pattern, std::function<void(void *, HttpRequest *)> connectHandler, std::function<void(uWS::WebSocket<SSL, true> *, std::string_view, uWS::OpCode)> messageHandler) { std::function<void(void *, HttpRequest *)> open = nullptr;
std::function<void(uWS::WebSocket<false, true> *, std::string_view, uWS::OpCode)> message = nullptr;
};
TemplatedApp &ws(std::string pattern, WebSocketBehavior &&behavior) {
// init the websocket context here! // init the websocket context here!
uWS::WebSocketContext<SSL, true> *webSocketContext = uWS::WebSocketContext<SSL, true>::create(uWS::Loop::defaultLoop(), (typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) httpContext); uWS::WebSocketContext<SSL, true> *webSocketContext = uWS::WebSocketContext<SSL, true>::create(uWS::Loop::defaultLoop(), (typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) httpContext);
webSocketContext->getExt()->messageHandler = messageHandler; webSocketContext->getExt()->messageHandler = behavior.message;
return get(pattern, [webSocketContext, this, connectHandler](auto *res, auto *req) { return get(pattern, [webSocketContext, this, behavior](auto *res, auto *req) {
std::string_view secWebSocketKey = req->getHeader("sec-websocket-key"); std::string_view secWebSocketKey = req->getHeader("sec-websocket-key");
if (secWebSocketKey.length()) { if (secWebSocketKey.length()) {
@@ -91,7 +97,7 @@ public:
); );
// we should hand the new socket to the handler // we should hand the new socket to the handler
connectHandler(webSocket, req); behavior.open(webSocket, req);
} else { } else {
@@ -105,6 +111,8 @@ public:
}); });
return *this;
} }
TemplatedApp &get(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) { TemplatedApp &get(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {