Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <uwebsockets/App.h>
- using namespace std;
- int main() {
- int latest_user_id = 10; // каждый раз увеличиваем на 1 (++)
- /* ws->getUserData returns one of these */
- struct UserData {
- int id;
- string name;
- };
- // .ws = конфигурация WebSocket сервера
- uWS::App({}).ws<UserData>("/*", {
- .idleTimeout = 16,
- /* Handlers */
- // Кто-то подключился к серверу
- .open = [](auto*/*ws*/) {
- /* Open event here, you may access ws->getUserData() which points to a PerSocketData struct */
- },
- // Кто-то отправил серверу пакет с данными
- .message = [](auto* ws, std::string_view message, uWS::OpCode opCode) {
- ws->send(message, opCode, true);
- },
- .close = [](auto*/*ws*/, int /*code*/, std::string_view /*message*/) {
- /* You may access ws->getUserData() here */
- }
- }).listen(9001, [](auto* listen_socket) {
- if (listen_socket) {
- std::cout << "Listening on port " << 9001 << std::endl;
- }
- }).run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement