forked from etr/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomet.cpp
More file actions
executable file
·66 lines (55 loc) · 2.51 KB
/
comet.cpp
File metadata and controls
executable file
·66 lines (55 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
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; either
version 2.1 of the License, or (at your option) any later version.
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 <httpserver.hpp>
#include <iostream>
#include <vector>
using namespace httpserver;
std::string topics_array[] = { "topic_1" };
std::vector<std::string> topics(topics_array, topics_array + sizeof(topics_array) / sizeof(std::string));
class comet_send_resource : public http_resource {
public:
void render(const http_request& req, http_response** res)
{
*res = new http_response(http_response_builder("Hi", 200).long_polling_send_response(topics_array[0]));
}
};
class comet_listen_resource : public http_resource {
public:
void render(const http_request& req, http_response** res)
{
*res = new http_response(http_response_builder("OK", 200).long_polling_receive_response(topics));
}
};
int main()
{
//it is possible to create a webserver passing a great number of parameters.
//In this case we are just passing the port and the number of thread running.
webserver ws = create_webserver(8080).comet();
comet_send_resource csr;
comet_listen_resource clr;
//this way we are registering the hello_world_resource to answer for the endpoint
//"/hello". The requested method is called (if the request is a GET we call the render_GET
//method. In case that the specific render method is not implemented, the generic "render"
//method is called.
ws.register_resource("/send", &csr, true);
ws.register_resource("/listen", &clr, true);
//This way we are putting the created webserver in listen. We pass true in order to have
//a blocking call; if we want the call to be non-blocking we can just pass false to the
//method.
ws.start(true);
return 0;
}