webcoket官网 可下载源码和实例
用到实例
1.broadcast_server.cpp
2.telemetry_client.cpp
telemetry_client.h
class telemetry_client {
public:
telemetry_client();
// This method will block until the connection is complete
void run(const std::string & uri);
// The open handler will signal that we are ready to start sending telemetry
void on_open(websocketpp::connection_hdl);
// The close handler will signal that we should stop sending telemetry
void on_close(websocketpp::connection_hdl);
// The fail handler will signal that we should stop sending telemetry
void on_fail(websocketpp::connection_hdl);
void send_h264(char* pH264Data, int length);
void send_text(char* text, int length);
private:
typedef websocketpp::client<websocketpp::config::asio_client> client;
typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
websocketpp::lib::mutex m_lock;
bool m_open;
bool m_done;
mutex m_action_lock;
public:
websocketpp::connection_hdl m_hdl;
client m_client;
bool connect = false;
};
telemetry_client.pp
//代码的关键在绑定on_message回调。
1.要在建立连接之前绑定。
2.客户端属性绑定on_message回调方法。且要放在建立连接之前。
m_client.set_message_handler(bind(&on_message, &m_client, ::_1, ::_2));
client::connection_ptr con = m_client.get_connection(uri, ec);
static void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg)
{
std::cout << "on_message called with hdl: " << hdl.lock().get() << endl;
std::cout<< "message: " << msg->get_payload().c_str()<< std::endl;
}
// This method will block until the connection is complete
void telemetry_client::run(const std::string & uri)
{
// Create a new connection to the given URI
websocketpp::lib::error_code ec;
//客户端属性绑定on_message回调方法。且要放在建立连接之前。
m_client.set_message_handler(bind(&on_message, &m_client, ::_1, ::_2));
client::connection_ptr con = m_client.get_connection(uri, ec);
if (ec) {
m_client.get_alog().write(websocketpp::log::alevel::app,
"Get Connection Error: " + ec.message());
return;
}
// Grab a handle for this connection so we can talk to it in a thread
// safe manor after the event loop starts.
m_hdl = con->get_handle();
// Queue the connection. No DNS queries or network connections will be
// made until the io_service event loop is run.
m_client.connect(con);
// Create a thread to run the ASIO io_service event loop
websocketpp::lib::thread asio_thread(&client::run, &m_client);
// Create a thread to run the telemetry loop
//websocketpp::lib::thread telemetry_thread(&telemetry_client::telemetry_loop, this);
//asio_thread.join();
connect = true;
asio_thread.detach();
//telemetry_thread.join();
}
// The open handler will signal that we are ready to start sending telemetry
void telemetry_client::on_open(websocketpp::connection_hdl)
{
m_client.get_alog().write(websocketpp::log::alevel::app,
"Connection opened, starting telemetry!");
scoped_lock guard(m_lock);
m_open = true;
}
// The close handler will signal that we should stop sending telemetry
void telemetry_client::on_close(websocketpp::connection_hdl)
{
m_client.get_alog().write(websocketpp::log::alevel::app,
"Connection closed, stopping telemetry!");
scoped_lock guard(m_lock);
m_done = true;
connect = false;
}
// The fail handler will signal that we should stop sending telemetry
void telemetry_client::on_fail(websocketpp::connection_hdl)
{
m_client.get_alog().write(websocketpp::log::alevel::app,
"Connection failed, stopping telemetry!");
scoped_lock guard(m_lock);
m_done = true;
}
void telemetry_client::send_h264(char* aacFrame, int length)
{
lock_guard<mutex> guard(m_action_lock);
websocketpp::lib::error_code ec;
m_client.send(m_hdl, aacFrame, length, websocketpp::frame::opcode::binary, ec);
if (ec) {
m_client.get_alog().write(websocketpp::log::alevel::app,
"Send Error: " + ec.message());
}
}
void telemetry_client::send_text(char * text, int length)
{
lock_guard<mutex> guard(m_action_lock);
websocketpp::lib::error_code ec;
m_client.send(m_hdl, text, length, websocketpp::frame::opcode::text, ec);
if (ec) {
m_client.get_alog().write(websocketpp::log::alevel::app,
"Send Error: " + ec.message());
}
}
int main()
{
telemetry_client connect_center_server_client;
connect_center_server_client.run("ws://192.168.18.19:9002/push/video/123");
char* smg = "post_deviceInfo";
int len = strlen(smg);
extern int deviceIndex;
websocketpp::lib::error_code ec;
while(deviceIndex == 0)
{
connect_center_server_client.send_text(smg, 15);
Sleep(3);
}
}