Multi-user gravity beats for Sound of Science 2013 (server)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 

66 lines
1.9 KiB

/*
* libwebsockets-test-echo - libwebsockets echo test implementation
*
* This implements both the client and server sides. It defaults to
* serving, use --client <remote address> to connect as client.
*
* Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
*
* 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 <map>
#include <string>
#include <lib/libwebsockets.h>
#include "websockets.h"
int uid = 0;
std::map<int, std::string> people_online;
struct User {
unsigned int index;
};
websockets::TestProtocol<User> 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);
}