I am currently trying to get a WebSocket Server Client running. The program connects to the server, waits for a start signal from another client and starts its routine. In the routine the program constantly produces a value for how far away a person is from the camera and should send this values back to the server. The problem is, that the sent message does not appear on the server, even through earlier in the code the exact same line works.
I already tried using a differnet thread for sending the message, but this did not resolve the problem. I am using Websocket++ with boost, asio.
#include <opencv2/dnn.hpp>
#include <librealsense2/rs.hpp>
#include "../cv-helpers.hpp"
#include <iostream>
#include <websocketpp/client.hpp>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
typedef websocketpp::client<websocketpp::config::asio_client> client;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
void async_send_handler(client* c, websocketpp::connection_hdl hdl, const std::string& message) {
try {
c->send(hdl, message, websocketpp::frame::opcode::text);
c->get_alog().write(websocketpp::log::alevel::app, "Sent Message: " + message);
}
catch (...) {
c->get_alog().write(websocketpp::log::alevel::app, "Send Failed");
}
}
void async_send_message(client* c, websocketpp::connection_hdl hdl, const std::string& message) {
boost::thread t(boost::bind(async_send_handler, c, hdl, message));
t.detach();
}
// Handlers
void on_open(client* c, websocketpp::connection_hdl hdl) {
std::string msg = "Hello";
c->send(hdl, msg, websocketpp::frame::opcode::text);
c->get_alog().write(websocketpp::log::alevel::app, "Sent Message: " + msg);
}
void on_fail(client* c, websocketpp::connection_hdl hdl) {
c->get_alog().write(websocketpp::log::alevel::app, "Connection Failed");
}
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
c->get_alog().write(websocketpp::log::alevel::app, "Received Reply: " + msg->get_payload());
if (msg->get_payload() == "Stop") {
async_send_message(c, hdl, "Bye, server!");
std::this_thread::sleep_for(std::chrono::seconds(1));
c->close(hdl, websocketpp::close::status::normal, "");
}
if (msg->get_payload() == "Start") {
////////////////////////////////
Camera Pipeline Code Loop [...]
////////////////////////////////
try {
async_send_message(c, hdl, distance);
}
catch (const std::exception& e) {
c->get_alog().write(websocketpp::log::alevel::app, "other exception");
std::cout << "Exception occurred: " << e.what() << std::endl;
}
c->get_alog().write(websocketpp::log::alevel::app, "Sent Message: " + distance);
//End of Loop
}
}
void on_close(client* c, websocketpp::connection_hdl hdl) {
c->get_alog().write(websocketpp::log::alevel::app, "Connection Closed");
}
int main(int argc, char* argv[]) {
client c;
std::string uri = "ws://localhost:8080";
if (argc == 2) {
uri = argv[1];
}
try {
// set logging policy if needed
c.clear_access_channels(websocketpp::log::alevel::frame_header);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
//c.set_error_channels(websocketpp::log::elevel::none);
// Initialize ASIO
c.init_asio();
// Register our handlers
c.set_open_handler(bind(&on_open, &c, ::_1));
c.set_fail_handler(bind(&on_fail, &c, ::_1));
c.set_message_handler(bind(&on_message, &c, ::_1, ::_2));
c.set_close_handler(bind(&on_close, &c, ::_1));
// Create a connection to the given URI and queue it for connection once
// the event loop starts
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
c.connect(con);
// Start the ASIO io_service run loop
c.run();
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
}
catch (...) {
std::cout << "other exception" << std::endl;
}
}
I tried using threads, thinking it kind if a synchron / asynchron problem, but the message with the distance still does not reach the server, but "Hello" at the opening of the connection and "Bye" when closing the connection do.