在ros项目中添加发送websocket wss消息的功能,修改如下代码并在CmakeLists.txt中添加依赖,实现将serialized_data发送到wss://autopilot-test.t3go.cn:443/api/v1/vehicle/push/message/LFB1FV696M2L43840。main.cpp:#include "ros/ros.h" #include "std_msgs/String.h" #include <boost/thread/locks.hpp> #include <boost/thread/shared_mutex.hpp> #include "third_party/apollo/proto/perception/perception_obstacle.pb.h" #include "t3_perception.pb.h" apollo::perception::PerceptionObstacles perception_obstacles_; void perceptionCallback(const std_msgs::String& msg) { ROS_WARN("t3 perceptionCallback parse"); if (perception_obstacles_.ParseFromString(msg.data)) { double timestamp = perception_obstacles_.header().timestamp_sec(); ROS_INFO("t3 perceptionCallback timestamp %f count:%d", timestamp, perception_obstacles_.perception_obstacle().size()); std::string data; perception_obstacles_.SerializeToString(&data); VehData veh_data; veh_data.set_messagetype(5); veh_data.set_messagedes("PerceptionObstacles"); veh_data.set_contents(data); std::string serialized_data; veh_data.SerializeToString(&serialized_data); } else { ROS_ERROR("t3 perceptionCallback parse fail!"); } } int main(int argc, char **argv) { ros::init(argc, argv, "listener"); ros::NodeHandle n; ros::Subscriber sub = n.subscribe("/perception_node/perception_objects", 1000, perceptionCallback); ros::spin(); return 0; }CMakeLists.txt:cmake_minimum_required(VERSION 3.0.2) project(t3) find_package(catkin REQUIRED COMPONENTS roscpp rospy pcl_ros std_msgs third_party ) find_package(Protobuf REQUIRED) include_directories(${Protobuf_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR}/..) find_package(Boost REQUIRED) include_directories(${Boost_INCLUDE_DIRS}) set(ixwebsocket_INCLUDE_DIR "/usr/local/include/ixwebsocket") set(ixwebsocket_LIBRARIES "/usr/local/lib/libixwebsocket.a") include_directories(${ixwebsocket_INCLUDE_DIR}) include_directories(${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_INCLUDE_DESTINATION}/${PROJECT_NAME}) include_directories(${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_INCLUDE_DESTINATION}/smartview) catkin_package(INCLUDE_DIRS ${PROJECT_INCLUDE_DIRS} DEPENDS ${GFLAGS_LIBRARIES} ) include_directories( ${catkin_INCLUDE_DIRS} ${PROTOBUF_INCLUDE_DIR} ${PROJECT_SOURCE_DIR}/.. ) add_executable(${PROJECT_NAME}_node src/main.cpp ) add_dependencies(${PROJECT_NAME}_node ${catkin_EXPORTED_TARGETS}) target_link_libraries(${PROJECT_NAME}_node ${catkin_LIBRARIES} ${PROTOBUF_LIBRARIES} smartview_proto ) install(TARGETS ${PROJECT_NAME}_node ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION} )
时间: 2024-03-15 15:47:07 浏览: 102
你可以使用以下代码来添加发送websocket wss消息的功能,同时在CmakeLists.txt中添加依赖:
```cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <boost/thread/locks.hpp>
#include <boost/thread/shared_mutex.hpp>
#include "third_party/apollo/proto/perception/perception_obstacle.pb.h"
#include "t3_perception.pb.h"
#include <ixwebsocket/IXWebSocket.h>
using namespace ix;
apollo::perception::PerceptionObstacles perception_obstacles_;
void perceptionCallback(const std_msgs::String& msg) {
ROS_WARN("t3 perceptionCallback parse");
if (perception_obstacles_.ParseFromString(msg.data)) {
double timestamp = perception_obstacles_.header().timestamp_sec();
ROS_INFO("t3 perceptionCallback timestamp %f count:%d", timestamp, perception_obstacles_.perception_obstacle().size());
std::string data;
perception_obstacles_.SerializeToString(&data);
VehData veh_data;
veh_data.set_messagetype(5);
veh_data.set_messagedes("PerceptionObstacles");
veh_data.set_contents(data);
std::string serialized_data;
veh_data.SerializeToString(&serialized_data);
// create a WebSocket connection
WebSocket websocket;
websocket.setTLSOptions({}); // use default options
websocket.setOnMessageCallback([](const WebSocketMessagePtr& msg) {
std::cout << "Received message: " << msg->str << std::endl;
});
websocket.setOnCloseCallback([](WebSocket& ws, const WebSocketCloseInfo& info) {
std::cout << "WebSocket closed: " << info.reason << std::endl;
});
websocket.setOnErrorCallback([](const std::error_code& ec) {
std::cout << "Error: " << ec << ", error message: " << ec.message() << std::endl;
});
websocket.connect("wss://autopilot-test.t3go.cn:443/api/v1/vehicle/push/message/LFB1FV696M2L43840");
websocket.send(serialized_data);
websocket.close();
} else {
ROS_ERROR("t3 perceptionCallback parse fail!");
}
}
int main(int argc, char **argv) {
ros::init(argc, argv, "listener");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/perception_node/perception_objects", 1000, perceptionCallback);
ros::spin();
return 0;
}
```
在CmakeLists.txt中添加以下依赖:
```
find_package(ixwebsocket REQUIRED)
target_link_libraries(${PROJECT_NAME}_node ixwebsocket)
```
注意,你需要安装ixwebsocket库以使用WebSocket功能。
阅读全文