1 #include <condition_variable>
3 #include <websocketpp/config/asio_no_tls_client.hpp>
5 #include <websocketpp/client.hpp>
9 #include <boost/thread/thread.hpp>
11 typedef websocketpp::client<websocketpp::config::asio_client> client;
13 using websocketpp::lib::placeholders::_1;
14 using websocketpp::lib::placeholders::_2;
15 using websocketpp::lib::bind;
17 // pull out the type of messages sent by our config
18 typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
20 // Create a server endpoint
26 void on_open(client* c, websocketpp::connection_hdl hdl) {
27 // now it is safe to use the connection
28 std::cout << "connection ready" << std::endl;
31 // Send a SIP OPTIONS message to the server:
32 std::string SIP_msg="OPTIONS sip:carol@chicago.com SIP/2.0\r\nVia: SIP/2.0/WS df7jal23ls0d.invalid;rport;branch=z9hG4bKhjhs8ass877\r\nMax-Forwards: 70\r\nTo: <sip:carol@chicago.com>\r\nFrom: Alice <sip:alice@atlanta.com>;tag=1928301774\r\nCall-ID: a84b4c76e66710\r\nCSeq: 63104 OPTIONS\r\nContact: <sip:alice@pc33.atlanta.com>\r\nAccept: application/sdp\r\nContent-Length: 0\r\n\r\n";
33 sip_client.send(hdl, SIP_msg.c_str(), websocketpp::frame::opcode::text);
36 void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
37 client::connection_ptr con = sip_client.get_con_from_hdl(hdl);
39 std::cout << "Received a reply:" << std::endl;
40 fwrite(msg->get_payload().c_str(), msg->get_payload().size(), 1, stdout);
44 int main(int argc, char* argv[]) {
46 std::string uri = "ws://localhost:9001";
53 // We expect there to be a lot of errors, so suppress them
54 sip_client.clear_access_channels(websocketpp::log::alevel::all);
55 sip_client.clear_error_channels(websocketpp::log::elevel::all);
58 sip_client.init_asio();
60 // Register our handlers
61 sip_client.set_open_handler(bind(&on_open,&sip_client,::_1));
62 sip_client.set_message_handler(bind(&on_message,&sip_client,::_1,::_2));
64 websocketpp::lib::error_code ec;
65 client::connection_ptr con = sip_client.get_connection(uri, ec);
67 // Specify the SIP subprotocol:
68 con->add_subprotocol("sip");
70 sip_client.connect(con);
72 // Start the ASIO io_service run loop
76 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
79 std::cout << "done" << std::endl;
81 } catch (websocketpp::exception const & e) {
82 std::cout << e.what() << std::endl;