From fa9d2a8478f3c90a95fcd8f770fbfb2656a52a76 Mon Sep 17 00:00:00 2001 From: talksik Date: Sat, 27 Jan 2024 12:07:29 -0800 Subject: [PATCH] working with dates in C++ --- .../index/Main.cpp.3A7DAD75EE1835F0.idx | Bin 0 -> 1514 bytes server/src/Main.cpp | 29 +++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 server/.cache/clangd/index/Main.cpp.3A7DAD75EE1835F0.idx diff --git a/server/.cache/clangd/index/Main.cpp.3A7DAD75EE1835F0.idx b/server/.cache/clangd/index/Main.cpp.3A7DAD75EE1835F0.idx new file mode 100644 index 0000000000000000000000000000000000000000..d0f1da011c6de464822ae77c8a71e2bf02567999 GIT binary patch literal 1514 zcmY+E4@?_X9LKNiwe-pzSK!K(zs=SbIL69Kun5#^$KL+pH90aLuYd*32&^HNq{Zg4ld9fv; zR{HP)dDRk)*G^PMyIZ73_}bZp_FLx~e%2&Ne|^*>!|dFyT^GwdN`<_^+Rs~hKV4)C zuF369caq~Ga!XND!S%ev&f{}s@-xyLQh9VZ`&wJR-L-Pz&yKf?ALI#!ZHq>pKp8Xj z;#|*3+g!}@!9i>5pmi*^_ExOKxco-LzU2Lf=doA*`LMKex)N>MsVm>^yn8vyDGNbY_I!zQ@_#M z;<((kKUI{L>*6rw0Y@}}-Sg&}^5QhG{{(tWdUxKGk0W9PA$TZHxZZ)0^%mRoqfP8s zbPEt13=92*()A7wn+yldso+R$b2>d~Ddlr;LQScqFzKlL%|=_=UJ_tt2a^f0t6(Cr z;}{vl#R)#fCm;rb3 z)H-t(z+qIF61q=GGH%TH-OB-3k}Ao8xWdnVJg2>IAHd;MIO~SP9MS*I*IQZXe&jb_ z1Kgoh=w|3LpTqkmyU&~eIFJmCg!p_AjSz*oNDWTDk(0>Mf?&ms}5o?6CTXQjWKii%=?{T;4t`Dw-E z2*4Uj!zLM;ZGGqTJ@pS9lwdSWDx4|k)z+xD(EDScQWfcn42Z!I?owq#^@YB<+WGv< z1c;?@N(}K?<4$x(R{hjkN`~1Eu^~l#fEGmjCPWDtEo0Y(5(--3qg1rYN5#|eJ}QY$ z@=?ijvX9czS|7Ddye$w;EFP8r;_bku!OlsHWUFLSRS|M+>tEAgnIua3L3i;;)vUu~ znFH=PDvmuTP1NG>Awkh17bW-se0Hy3iyIlX9tV0xUKV-YT>-IDN-BpbQxJESQ%tP_ zEGdwfAT}9{MQito*;HbduYHhf-Fo8?=fkd!%cgH=xB(;*05O7;l$RR8g@g!2A})_F J@JIbP*uO~$8#Dj_ literal 0 HcmV?d00001 diff --git a/server/src/Main.cpp b/server/src/Main.cpp index 7d4a007..bc527a1 100644 --- a/server/src/Main.cpp +++ b/server/src/Main.cpp @@ -1,5 +1,23 @@ +#include +#include +#include #include #include +#include + +namespace DateTime +{ +std::chrono::system_clock::time_point now() +{ + return std::chrono::system_clock::now(); +} + +void outputTime(const char *desc, std::chrono::system_clock::time_point &tp) +{ + std::time_t now = std::chrono::system_clock::to_time_t(tp); + std::cout << desc << std::put_time(std::localtime(&now), "%Y-%m-%d %H:%M") << "\n"; +} +} // namespace DateTime int main() { @@ -8,7 +26,7 @@ int main() /* ws->getUserData returns one of these */ struct PerSocketData { - /* Fill with user data */ + std::chrono::system_clock::time_point created_at; }; /* Keep in mind that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support. @@ -29,15 +47,18 @@ int main() /* Handlers */ .upgrade = nullptr, .open = - [](auto * /*ws*/) { - /* Open event here, you may access ws->getUserData() which points to a PerSocketData struct - */ + [](uWS::WebSocket *ws) { + auto user_data = ws->getUserData(); + user_data->created_at = DateTime::now(); + + DateTime::outputTime("Client connected at ", user_data->created_at); }, .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) { /* This is the opposite of what you probably want; compress if message is LARGER than 16 kb * the reason we do the opposite here; compress if SMALLER than 16 kb is to allow for * benchmarking of large message sending without compression */ + std::cout << "Message received: " << message << std::endl; ws->send(message, opCode, message.length() < 16 * 1024); }, .dropped =