I need to set up an application that listens on a number of Urls but do not know when I start how many - this will eventually be read from a database but for the moment they are hard coded in a demo.
// MultipleListenerTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <vector>
#include <cpprest\http_listener.h>
#include <cpprest\http_client.h>
#include "TestListener.h" // listener object
using namespace web::http::client;
using namespace web::http::experimental::listener;
using namespace web::http;
using namespace web;
void SetListenerArray();
typedef std::vector<http_listener> httpListeners;
listenerCollection listeners;
TestListener listener1;
TestListener Listener2;
TestListener Listener3;
httpListeners httpListenersList;
void handle_get(http_request request);
void handle_post(http_request request);
void handle_put(http_request request);
void handle_del(http_request request);
int _tmain(int argc, _TCHAR* argv[])
{
SetListenerArray();
for each (TestListener tl in listeners)
{
http_listener l(tl.GetUrl());
l.support(methods::GET, handle_get);
l.support(methods::POST, handle_post);
l.support(methods::PUT, handle_put);
l.support(methods::DEL, handle_del);
l.open().wait();
httpListenersList.push_back(l);
}
std::string line;
std::cout << "Press enter to exit" << std::endl;
std::getline(std::cin, line);
//listenerVoices_1.close().wait();
//listenerVoices_2.close().wait();
return 0;
}
void SetListenerArray()
{
// first listener
listener1.SetListenerName(to_string_t("FirstListener"));
listener1.SetUrl(to_string_t("http://localhost:8010"));
listeners.push_back(listener1);
// second listener
Listener2.SetListenerName(to_string_t("Second Listener"));
Listener2.SetUrl(to_string_t("http://localhost:8020"));
listeners.push_back(Listener2);
//third listener
Listener3.SetListenerName(to_string_t("Third Listener"));
Listener3.SetUrl(to_string_t("http://localhost:8030"));
listeners.push_back(Listener3);
}
void handle_get(http_request request)
{
for each (TestListener tl in listeners)
{
std::cout << to_utf8string(tl.GetListenerName());
}
}
void handle_post(http_request request)
{
}
void handle_put(http_request request)
{
}
void handle_del(http_request request)
{
}
With the above code the line
httpListenersList.push_back(l);
fails to compile with the error
Error 1 error C2248: 'web::http::experimental::listener::http_listener::http_listener' : cannot access private member declared in class 'web::http::experimental::listener::http_listener' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0 593 1 MultipleListenerTest
If I leave it out then none of the listeners are actually listening and you get This webpage is not available in Chrome at least.
So how can you set up multiple listeners when you do not know before hand how many are required?