void ServerAcceptWebHelper::on_message(websocketpp::connection_hdl hdl, server::message_ptr msg) { if (msg->get_opcode() == websocketpp::frame::opcode::BINARY) { SessionMap::iterator pos = m_sessionMap.find(hdl); if (pos != m_sessionMap.end()) { boost::shared_ptr<SessionData> tmpSession = pos->second.lock(); if (tmpSession) { tmpSession->GetReceiveBuffer().HandleMessage(tmpSession, msg->get_payload()); } } } }
void ControlServer::on_message(websocketpp::connection_hdl hdl, server::message_ptr message_ptr) { log_->trace() << log_tag() << BOOST_CURRENT_FUNCTION; try { log_->trace() << message_ptr->get_payload(); // Read as control_json; Json::Value control_json; Json::Reader r; r.parse(message_ptr->get_payload(), control_json); dispatch_control_json(control_json); }catch(std::exception& e) { log_->trace() << log_tag() << "on_message e:" << e.what(); ws_server_.get_con_from_hdl(hdl)->close(websocketpp::close::status::protocol_error, e.what()); } }
void IEDiagnosticsAdapter::OnMessage(websocketpp::connection_hdl hdl, server::message_ptr msg) { if (m_clientConnections.find(hdl) != m_clientConnections.end()) { // Message from WebKit client to IE CString message(msg->get_payload().c_str()); this->SendMessageToInstance(m_clientConnections[hdl], message); } }
void Webserver::handleReceivedMessage(websocketpp::connection_hdl handle, Server::message_ptr msg) { using namespace json11; std::string error; auto doc = Json::parse(msg->get_payload().c_str(), error); if(doc.is_null()) { std::cerr << "bad message received: " << msg->get_payload() << std::endl; return; } auto const& cmd = doc["cmd"]; if(!cmd.is_string()) { std::cerr << "Bad message received: " << msg->get_payload() << std::endl; return; } CommandHandler handler; { std::lock_guard<std::mutex> lock(m_commandsMutex); auto it = m_commands.find(cmd.string_value()); if(it == m_commands.end()) { std::cerr << "Unexpected command received: " << cmd.string_value() << std::endl; return; } handler = it->second; } handler(doc["args"], [this, handle](std::string const& message) { websocketpp::lib::error_code ec; auto connection = m_server.get_con_from_hdl(handle, ec); if(ec || !connection) { std::cerr << "Error sending message: " << ec.message() << std::endl; return false; } ec = connection->send(message); if(ec) { std::cerr << "Error sending message: " << ec.message() << std::endl; return false; } return true; }); }
void on_message(connection_hdl hdl, server::message_ptr msg) { connection_data& data = get_data_from_hdl(hdl); if (data.name.empty()) { data.name = msg->get_payload(); std::cout << "Setting name of connection with sessionid " << data.sessionid << " to " << data.name << std::endl; } else { std::cout << "Got a message from connection " << data.name << " with sessionid " << data.sessionid << std::endl; } }
void messageReceived(Server* server, websocketpp::connection_hdl hdl, Server::message_ptr message) { messageCount++; std::string payload = message->get_payload(); #ifndef NDEBUG std::cout << "received: " << messageCount << std::endl; #endif // killServer message if (payload == "killServer") { std::cout << "killServer" << std::endl; server->stop_listening(); return; } // send received message back try { server->send(hdl, message->get_payload(), message->get_opcode()); } catch (const websocketpp::lib::error_code& e) { std::cout << "send failed: " << e << ":" << e.message() << std::endl; } }
void Webserver::onMessage(connection_hdl handle, server::message_ptr message){ auto con = socketServer.get_con_from_hdl(handle); std::string subp = con->get_subprotocol(); if (clientMessages.count(subp) == 0) { // Create queue for subprotocol boost::unique_lock<boost::shared_mutex> lock(serverMutex); clientMessages.insert({subp, new boost::lockfree::queue<ClientMessage *>(300)}); } boost::shared_lock<boost::shared_mutex> lock(serverMutex); auto messages = clientMessages.at(subp); int sessionId = con->sessionInfo->sessionId; auto queueElement = new ClientMessage(); queueElement->type = ClientMessage::Type::MESSAGE; queueElement->message = message->get_payload(); queueElement->sessionId = sessionId; messages->push(queueElement); }
/** * Is called when the message is received by the server * @param hdl the connection handler * @param raw_msg the received message */ virtual void on_message(websocketpp::connection_hdl hdl, server::message_ptr raw_msg) { LOG_DEBUG << "Received a message!" << END_LOG; //Create an empty json message incoming_msg * jmsg = new incoming_msg(); //De-serialize the message and then handle based on its type try { string raw_msg_str = raw_msg->get_payload(); LOG_DEBUG << "Received JSON msg: " << raw_msg_str << END_LOG; //De-serialize the message jmsg->de_serialize(raw_msg_str); //Handle the request message based on its type switch (jmsg->get_msg_type()) { case msg_type::MESSAGE_TRANS_JOB_REQ: translation_request(hdl, new trans_job_req_in(jmsg)); break; case msg_type::MESSAGE_SUPP_LANG_REQ: language_request(hdl, new supp_lang_req_in(jmsg)); break; case msg_type::MESSAGE_PRE_PROC_JOB_REQ: pre_process_request(hdl, new proc_req_in(jmsg)); break; case msg_type::MESSAGE_POST_PROC_JOB_REQ: post_process_request(hdl, new proc_req_in(jmsg)); break; default: THROW_EXCEPTION(string("Unsupported request type: ") + to_string(jmsg->get_msg_type())); } } catch (std::exception & e) { //Send the error response, NOTE! This is not a JSON we are sending //back, but just a string, as someone violated the protocol! send_response(hdl, e.what()); } }
void on_message(websocketpp::connection_hdl hdl, server::message_ptr msg) { try { //cout << "message" << endl; //immediatly send back a message for timing purposes //websocket_server.send(hdl ,"TIME",websocketpp::frame::opcode::text); //parse the json ptree parsedJson; stringstream jsonStream; jsonStream << msg->get_payload(); read_json(jsonStream, parsedJson); if (parsedJson.get<bool>("triangle")) { cout << "Received triangle" << endl; shutDown(); } //toggle autonomous mode if (parsedJson.get<bool>("start")) { pthread_mutex_lock(&autonomous_mutex); if (currentState.autonomous == true) { currentState.autonomous = false; cout << "Autonomous mode off." << endl; } else { currentState.autonomous = true; cout << "Autonomous mode on." << endl; } pthread_mutex_unlock(&autonomous_mutex); } pthread_mutex_lock(&autonomous_mutex); if (currentState.autonomous != true) { //update the servo values currentState.servoInt = (int) (SERVO_CENTER + (parsedJson.get<double>("leftAnalog_x") * MAX_STEERING_OFFSET)); if (parsedJson.get<double>("rightAnalog_y") < 0) { currentState.cruise = false; } if (currentState.cruise == false) { //skip the center range int requestedMotorInt = 0; pthread_mutex_lock(&obsticle_mutex); if ((parsedJson.get<double>("rightAnalog_y") > 0) && (currentState.obsticle != true)) { //forward //cout << "rightAnalog_y: " << parsedJson.get<double>("rightAnalog_y") << endl; requestedMotorInt = (int) (parsedJson.get<double>( "rightAnalog_y") * MOTOR_FORWARD_RANGE); //cout << "requestedMotirInt(forward): " << requestedMotorInt << endl; if ((requestedMotorInt - currentState.lastRequestedMotorInt) > ACCELERATION_THRESHOLD) { requestedMotorInt = currentState.lastRequestedMotorInt + ACCELERATION_THRESHOLD; //cout << "Don't accelerate so quickly!" << endl; } currentState.motorInt = MOTOR_FORWARD_START + requestedMotorInt; } else if (parsedJson.get<double>("rightAnalog_y") < 0) { //backward //cout << "rightAnalog_y: " << parsedJson.get<double>("rightAnalog_y") << endl; requestedMotorInt = -1 * (int) (parsedJson.get<double>("rightAnalog_y") * MOTOR_BACKWARD_RANGE); //cout << "requestedMotirInt(backward): " << requestedMotorInt << endl; if ((currentState.lastRequestedMotorInt - requestedMotorInt) > ACCELERATION_THRESHOLD) { requestedMotorInt = currentState.lastRequestedMotorInt - ACCELERATION_THRESHOLD; //cout << "Don't accelerate (backwards) so quickly!" << endl; } currentState.motorInt = MOTOR_BACKWARD_START - requestedMotorInt; if (currentState.cruise) { currentState.cruise = false; // cout << "Cruise mode off." << endl; } } else { //stop currentState.motorInt = SERVO_CENTER; } pthread_mutex_unlock(&obsticle_mutex); //cout << "updating lastRequestedMotorInt: " << currentState.lastRequestedMotorInt << endl; currentState.lastRequestedMotorInt = requestedMotorInt; //update motor //cout << "Current value of motorInt: " << currentState.motorInt << endl; gpioServo(GPIO_MOTOR_CONTROL, currentState.motorInt); } //update servo gpioServo(GPIO_STEERING_CONTROL, currentState.servoInt); //toggle turn signal //right pthread_mutex_lock(&turnSignal_mutex); if (parsedJson.get<bool>("R1")) { if (currentState.rightTurn == true) { currentState.rightTurn = false; currentState.leftTurn = false; //cout << "Right turn signal off." << endl; } else { currentState.rightTurn = true; currentState.leftTurn = false; //cout << "Right turn signal on." << endl; } } //left if (parsedJson.get<bool>("L1")) { if (currentState.leftTurn == true) { currentState.leftTurn = false; currentState.rightTurn = false; //cout << "Left turn signal off." << endl; } else { currentState.leftTurn = true; currentState.rightTurn = false; //cout << "Left turn signal on." << endl; } } //check for peak if (currentState.leftTurn || currentState.rightTurn) { if (currentState.turnPeaked) { if (currentState.servoInt == SERVO_CENTER) { currentState.leftTurn = false; currentState.rightTurn = false; currentState.turnPeaked = false; //cout << "Turn complete" << endl; } } else { if (currentState.leftTurn) { if (currentState.servoInt < (SERVO_CENTER - TURN_THRESHOLD)) { currentState.turnPeaked = true; } } else if (currentState.rightTurn) { if (currentState.servoInt > (SERVO_CENTER + TURN_THRESHOLD)) { currentState.turnPeaked = true; } } } } pthread_mutex_unlock(&turnSignal_mutex); //toggle cruise mode if (parsedJson.get<bool>("left")) { if (currentState.cruise == true) { currentState.cruise = false; cout << "Cruise mode off" << endl; } else { currentState.cruise = true; cout << "Cruise mode on" << endl; } } if (parsedJson.get<bool>("up")) { currentState.motorInt = currentState.motorInt + 10; gpioServo(GPIO_MOTOR_CONTROL, currentState.motorInt); //cout << "Current value of motorInt: " << currentState.motorInt << endl; } else if (parsedJson.get<bool>("down")) { currentState.motorInt = currentState.motorInt - 10; gpioServo(GPIO_MOTOR_CONTROL, currentState.motorInt); //cout << "Current value of motorInt: " << currentState.motorInt << endl; } } pthread_mutex_unlock(&autonomous_mutex); ptree answerJson; stringstream jsonWriteStream; answerJson.put("left_blinker", currentState.leftTurn); answerJson.put("right_blinker", currentState.rightTurn); write_json(jsonWriteStream, answerJson); //cout << "sending speedometer" << endl; if (currentState.connected) { websocket_server.send(currentState.client, jsonWriteStream.str(), websocketpp::frame::opcode::text); } //keep track of message recieved times. unsigned long currentTime = time(NULL); pthread_mutex_lock(&watchDog_mutex); //cout << "currentTime=" << currentTime << endl; currentState.lastMessageTime = currentTime; currentState.connected = true; pthread_mutex_unlock(&watchDog_mutex); } catch (std::exception const& e) { cerr << "<ERROR>" << endl; cerr << e.what() << endl; cerr << msg->get_payload() << endl; cerr << "</ERROR>" << endl; } }
void on_message(websocketpp::connection_hdl, server::message_ptr msg) { std::cout << msg->get_payload() << std::endl; }
void echo_handler(websocketpp::connection_hdl hdl, server::message_ptr msg) { // write a new message m_endpoint.send(hdl, msg->get_payload(), msg->get_opcode()); }
void on_message(connection_hdl hdl, server::message_ptr msg) {//make sure to start a new thread for long running processes! std::cout<<msg->get_payload()<<std::endl; //std::thread myThread(&somefunction, this, hdl, msg->get_payload()); }
void echo_handler(websocketpp::connection_hdl hdl, server::message_ptr msg) { // write a new message std::string str = msg->get_payload(); printf("\nrec: %s\n",str.c_str()); m_endpoint.send(hdl, msg->get_payload(), msg->get_opcode()); }
void WebsocketServer::on_message(connection_hdl hdl, server::message_ptr msg, Database database) { connection_ptr con = m_server.get_con_from_hdl(hdl); nlohmann::json jdata; std::string payload = msg->get_payload(); try { jdata.clear(); jdata = nlohmann::json::parse(payload); if (jdata["type"] == "lgn") { std::string lname = jdata["data"]; if (con->name == "") { con->name = lname; std::cout << "Setting name with session id " << con->session_id << " to " << lname << std::endl; } else { std::cout << "Got message from connection " << lname << " with session id " << con->session_id << std::endl; } } if (jdata["type"] == "msg") { std::string clientmsg = jdata["data"]; jdata["cnt"] = clientmsg.length(); msg->set_payload(jdata.dump()); m_server.send(hdl, msg); } // Show table if (jdata["type"] == "get table") { nlohmann::json message; // Get table message.clear(); message = database.get_table(jdata); msg->set_payload(message.dump()); m_server.send(hdl, msg); // Get matches message.clear(); message = database.get_matches(jdata); msg->set_payload(message.dump()); m_server.send(hdl, msg); // Get finished matches message.clear(); message = database.get_finished_matches(jdata); msg->set_payload(message.dump()); m_server.send(hdl, msg); // Get coming matches message.clear(); message = database.get_coming_matches(jdata); msg->set_payload(message.dump()); m_server.send(hdl, msg); } // Goal scored if (jdata["type"] == "goal") { nlohmann::json table; nlohmann::json matches; unsigned int hts = jdata["hometeam_score"]; unsigned int ats = jdata["awayteam_score"]; std::string minusone = "-1"; std::string minustwo = "-2"; std::string zero = "0"; std::string one = "1"; std::string two = "2"; // If it was equal score and hometeam score. And shuffle points. if (hts == ats && jdata["scoringteam"] == "hometeam") { // Add two points to the hometeam. And subtract one from the awayteam. jdata["points"] = two; jdata["team"] = jdata["hometeam"]; jdata["won"] = one; jdata["draw"] = minusone; jdata["lost"] = zero; database.update_standing(jdata); jdata["points"] = minusone; jdata["team"] = jdata["awayteam"]; jdata["won"] = zero; jdata["draw"] = minusone; jdata["lost"] = one; database.update_standing(jdata); } // If it was equal score and awayteam score. And shuffle points. if (hts == ats && jdata["scoringteam"] == "awayteam") { // Subtract one point from the hometeam and add two to the awayteam. jdata["points"] = minusone; jdata["team"] = jdata["hometeam"]; jdata["won"] = zero; jdata["draw"] = minusone; jdata["lost"] = one; database.update_standing(jdata); jdata["points"] = two; jdata["team"] = jdata["awayteam"]; jdata["won"] = one; jdata["draw"] = minusone; jdata["lost"] = zero; database.update_standing(jdata); } // If hometeam is down by one and scores shuffle points. if ((hts - ats) == -1 && jdata["scoringteam"] == "hometeam") { // Add one point to hometeam and subtract two points from awayteam. jdata["points"] = one; jdata["team"] = jdata["hometeam"]; jdata["won"] = zero; jdata["draw"] = one; jdata["lost"] = minusone; database.update_standing(jdata); jdata["points"] = minustwo; jdata["team"] = jdata["awayteam"]; jdata["won"] = minusone; jdata["draw"] = one; jdata["lost"] = zero; database.update_standing(jdata); } // If awayteam is down by one and scores shuffle points. if ((hts - ats) == 1 && jdata["scoringteam"] == "awayteam") { // Subtract two points from hometeam and add one point to awayteam. jdata["points"] = minustwo; jdata["team"] = jdata["hometeam"]; jdata["won"] = minusone; jdata["draw"] = one; jdata["lost"] = zero; database.update_standing(jdata); jdata["points"] = one; jdata["team"] = jdata["awayteam"]; jdata["won"] = zero; jdata["draw"] = one; jdata["lost"] = minusone; database.update_standing(jdata); } // Add goal to matches- and teams-table. if (jdata["scoringteam"] == "hometeam") { jdata["team_score"] = "hometeam_score"; database.update_goalscore(jdata); } else { jdata["team_score"] = "awayteam_score"; database.update_goalscore(jdata); } // Get table table.clear(); table = database.get_table(jdata); // Get matches matches.clear(); matches = database.get_matches(jdata); // Broadcast to clients for (auto it : m_connections) { msg->set_payload(table.dump()); m_server.send(it, msg); msg->set_payload(matches.dump()); m_server.send(it, msg); } } // Goal canceled if (jdata["type"] == "cancelgoal") { nlohmann::json table; nlohmann::json matches; unsigned int hts = jdata["hometeam_score"]; unsigned int ats = jdata["awayteam_score"]; const std::string minusone = "-1"; const std::string minustwo = "-2"; const std::string zero = "0"; const std::string one = "1"; const std::string two = "2"; // Don't cancel goal if score is zero. if ((jdata["scoringteam"] == "hometeam" && hts > 0) || (jdata["scoringteam"] == "awayteam" && ats > 0)) { // If it was equal score and hometeam have a goal cancelled. Shuffle points. if (hts == ats && jdata["scoringteam"] == "hometeam") { // Subtract one point from the hometeam. And add two to the awayteam. jdata["points"] = minusone; jdata["team"] = jdata["hometeam"]; jdata["won"] = zero; jdata["draw"] = minusone; jdata["lost"] = one; database.update_standing(jdata); jdata["points"] = two; jdata["team"] = jdata["awayteam"]; jdata["won"] = one; jdata["draw"] = minusone; jdata["lost"] = zero; database.update_standing(jdata); } // If it was equal score and awayteam have a goal cancellled. Shuffle points. if (hts == ats && jdata["scoringteam"] == "awayteam") { // Add two points to the hometeam and subtract one from the awayteam. jdata["points"] = two; jdata["team"] = jdata["hometeam"]; jdata["won"] = one; jdata["draw"] = minusone; jdata["lost"] = zero; database.update_standing(jdata); jdata["points"] = minusone; jdata["team"] = jdata["awayteam"]; jdata["won"] = zero; jdata["draw"] = minusone; jdata["lost"] = zero; database.update_standing(jdata); } // If hometeam is up by one and goal is cancelled shuffle points. if ((hts - ats) == 1 && jdata["scoringteam"] == "hometeam") { // Subtract two points from hometeam and add one point to awayteam. jdata["points"] = minustwo; jdata["team"] = jdata["hometeam"]; jdata["won"] = minusone; jdata["draw"] = one; jdata["lost"] = zero; database.update_standing(jdata); jdata["points"] = one; jdata["team"] = jdata["awayteam"]; jdata["won"] = zero; jdata["draw"] = one; jdata["lost"] = minusone; database.update_standing(jdata); } // If awayteam is up by one and goal is cancelled shuffle points. if ((hts - ats) == -1 && jdata["scoringteam"] == "awayteam") { // Add one point to hometeam and subtract two points from awayteam. jdata["points"] = one; jdata["team"] = jdata["hometeam"]; jdata["won"] = zero; jdata["draw"] = one; jdata["lost"] = minusone; database.update_standing(jdata); jdata["points"] = minustwo; jdata["team"] = jdata["awayteam"]; jdata["won"] = minusone; jdata["draw"] = one; jdata["lost"] = zero; database.update_standing(jdata); } // Remove goal to matches- and teams-table. if (jdata["scoringteam"] == "hometeam") { jdata["team_score"] = "hometeam_score"; database.update_goalscore(jdata); } else { jdata["team_score"] = "awayteam_score"; database.update_goalscore(jdata); } // Get table table.clear(); table = database.get_table(jdata); // Get matches matches.clear(); matches = database.get_matches(jdata); // Broadcast to clients for (auto it : m_connections) { msg->set_payload(table.dump()); m_server.send(it, msg); msg->set_payload(matches.dump()); m_server.send(it, msg); } } } // Start match if (jdata["type"] == "start_match") { nlohmann::json table; nlohmann::json matches; nlohmann::json coming_matches; const std::string zero = "0"; const std::string one = "1"; jdata["points"] = one; jdata["won"] = zero; jdata["draw"] = one; jdata["lost"] = zero; jdata["team"] = jdata["hometeam"]; database.update_standing(jdata); jdata["team"] = jdata["awayteam"]; database.update_standing(jdata); database.start_match(jdata); // Get table table.clear(); table = database.get_table(jdata); // Get matches matches.clear(); matches = database.get_matches(jdata); // Get coming matches coming_matches.clear(); coming_matches = database.get_coming_matches(jdata); // Broadcast to clients for (auto it : m_connections) { msg->set_payload(table.dump()); m_server.send(it, msg); msg->set_payload(matches.dump()); m_server.send(it, msg); msg->set_payload(coming_matches.dump()); m_server.send(it, msg); } } // End match if (jdata["type"] == "end_match") { nlohmann::json matches; nlohmann::json coming_matches; nlohmann::json finished_matches; database.end_match(jdata); // Get matches matches.clear(); matches = database.get_matches(jdata); // Get coming matches coming_matches.clear(); coming_matches = database.get_coming_matches(jdata); // Get finished matches finished_matches.clear(); finished_matches = database.get_finished_matches(jdata); // Broadcast to clients for (auto it : m_connections) { msg->set_payload(matches.dump()); m_server.send(it, msg); msg->set_payload(coming_matches.dump()); m_server.send(it, msg); msg->set_payload(finished_matches.dump()); m_server.send(it, msg); } } // Get coming matches if (jdata["type"] == "get_coming_matches") { nlohmann::json message; message.clear(); message = database.get_coming_matches(jdata); msg->set_payload(message.dump()); for (auto it : m_connections) { m_server.send(it, msg); } } // Get finished matches if (jdata["type"] == "get_finished_matches") { nlohmann::json message; message.clear(); message = database.get_finished_matches(jdata); msg->set_payload(message.dump()); for (auto it : m_connections) { m_server.send(it, msg); } } // Get matches with no startdate if (jdata["type"] == "get_matches_without_startdate") { nlohmann::json message; message.clear(); message = database.get_matches_without_startdate(jdata); msg->set_payload(message.dump()); for (auto it : m_connections) { m_server.send(it, msg); } } // Set matchdate if (jdata["type"] == "set_match_date") { nlohmann::json matches_without_startdate; nlohmann::json coming_matches; database.set_matchdate(jdata); // Get matches without startdate matches_without_startdate.clear(); matches_without_startdate = database.get_matches_without_startdate(jdata); // Get coming matches coming_matches.clear(); coming_matches = database.get_coming_matches(jdata); // Broadcast to clients for (auto it : m_connections) { msg->set_payload(matches_without_startdate.dump()); m_server.send(it, msg); msg->set_payload(coming_matches.dump()); m_server.send(it, msg); } } } catch (const std::exception& e) { msg->set_payload("Unable to parse json"); m_server.send(hdl, msg); std::cerr << "Unable to parse json: " << e.what() << std::endl; } }