BVB Source Codes

uWebSockets Show multithreaded_echo.cpp Source code

Return Download uWebSockets: download multithreaded_echo.cpp Source code - Download uWebSockets Source code - Type:.cpp
  1. #include <uWS/uWS.h>
  2. #include <thread>
  3. #include <algorithm>
  4. #include <iostream>
  5.  
  6. int main() {
  7.     std::vector<std::thread *> threads(std::thread::hardware_concurrency());
  8.     std::transform(threads.begin(), threads.end(), threads.begin(), [](std::thread *t) {
  9.         return new std::thread([]() {
  10.             uWS::Hub h;
  11.  
  12.             h.onMessage([](uWS::WebSocket<uWS::SERVER> *ws, char *message, size_t length, uWS::OpCode opCode) {
  13.                 ws->send(message, length, opCode);
  14.             });
  15.  
  16.             // This makes use of the SO_REUSEPORT of the Linux kernel
  17.             // Other solutions include listening to one port per thread
  18.             // with or without some kind of proxy inbetween
  19.             if (!h.listen(3000, nullptr, uS::ListenOptions::REUSE_PORT)) {
  20.                 std::cout << "Failed to listen" << std::endl;
  21.             }
  22.             h.run();
  23.         });
  24.     });
  25.  
  26.     std::for_each(threads.begin(), threads.end(), [](std::thread *t) {
  27.         t->join();
  28.     });
  29. }
  30.  
downloadmultithreaded_echo.cpp Source code - Download uWebSockets Source code
Related Source Codes/Software:
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11

 Back to top