I have downloaded ASP.NET Core SignalR C++ Client (1.0-alpha2-9073dc8645) using vcpkg and built it. I trying to compile the sample example HubConnectionSample.cpp in VS 2019 but getting following errors and warnings
Severity Code Description Project File Line Suppression State
Error (active) E0312 no suitable user-defined conversion from "std::shared_ptr" to "std::shared_ptrsignalr::log_writer" exists SignalRApp …SignalRAppSignalRAppSignalRApp.cpp 54
Severity Code Description Project File Line Suppression State
Error C2011 ‘signalr::log_writer’: ‘class’ type redefinition SignalRApp ..SignalRApppackagesMicrosoft.AspNet.SignalR.Client.Cpp.v140.WinDesktop.1.0.0-beta2buildnativeincludesignalrclientlog_writer.h 11
log_writer.h is from the SignalR package. My piece of code is
#include <iostream>
#include <sstream>
#include <signalrclienthub_connection_builder.h>
#include <signalrclientlog_writer.h>
#include <future>
#include <signalrclientsignalr_value.h>
class logger : public signalr::log_writer
{
// Inherited via log_writer
virtual void __cdecl write(const std::string& entry) override
{
std::cout << entry;
}
};
void send_message(signalr::hub_connection& connection, const std::string& message)
{
std::vector<signalr::value> arr{ std::string("c++"), message };
signalr::value args(arr);
// if you get an internal compiler error uncomment the lambda below or install VS Update 4
connection.invoke("Send", args, [](const signalr::value& value, std::exception_ptr exception)
{
try
{
if (exception)
{
std::rethrow_exception(exception);
}
if (value.is_string())
{
std::cout << "Received: " << value.as_string() << std::endl;
}
else
{
std::cout << "hub method invocation has completed" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "Error while sending data: " << e.what() << std::endl;
}
});
}
void chat()
{
signalr::hub_connection connection =
signalr::hub_connection_builder::create("http://localhost:5000/default")
.with_logging(std::make_shared <logger>(), signalr::trace_level::verbose)
.build();
connection.on("Send", [](const signalr::value& m)
{
std::cout << std::endl << m.as_array()[0].as_string() << std::endl << "Enter your message: ";
});
std::promise<void> task;
connection.start([&connection, &task](std::exception_ptr exception)
{
if (exception)
{
try
{
std::rethrow_exception(exception);
}
catch (const std::exception& ex)
{
std::cout << "exception when starting connection: " << ex.what() << std::endl;
}
task.set_value();
return;
}
std::cout << "Enter your message:";
while (connection.get_connection_state() == signalr::connection_state::connected)
{
std::string message;
std::getline(std::cin, message);
if (message == ":q" || connection.get_connection_state() !=
signalr::connection_state::connected)
{
break;
}
send_message(connection, message);
}
connection.stop([&task](std::exception_ptr exception)
{
try
{
if (exception)
{
std::rethrow_exception(exception);
}
std::cout << "connection stopped successfully" << std::endl;
}
catch (const std::exception& e)
{
std::cout << "exception when stopping connection: " << e.what() << std::endl;
}
task.set_value();
});
});
task.get_future().get();
}
int main()
{
chat();
return 0;
}
}**strong text**
Source: Windows Questions C++