/* * libwebsockets-test-echo - libwebsockets echo test implementation * * This implements both the client and server sides. It defaults to * serving, use --client to connect as client. * * Copyright (C) 2010-2013 Andy Green * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation: * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA */ #include #include #include #include "websockets.h" int uid = 0; std::map people_online; struct User { unsigned int index; }; websockets::TestProtocol default_protocol{ [](User& user) -> int{ user.index = uid++; people_online[user.index] = ""; return 0; }, [](User& user) -> int{ people_online.erase(user.index); return 0; }, [](User& user) -> std::string{ std::string string_to_send = "People online: " + std::to_string(people_online.size()); for(auto x : people_online) string_to_send += ", " + x.second; return string_to_send; }, [](User& user, std::string in) -> int{ people_online[user.index] = in; return 0; } }; static struct libwebsocket_protocols protocols[] = { WSstandard_protocol("default", default_protocol, User), { NULL, NULL, 0 } // end of list }; int main(int argc, char **argv){ return websockets::default_main(argc, argv, protocols); }