diff --git a/examples/sip_client/CMakeLists.txt b/examples/sip_client/CMakeLists.txt new file mode 100644 index 0000000000..3ac386d562 --- /dev/null +++ b/examples/sip_client/CMakeLists.txt @@ -0,0 +1,11 @@ + +file (GLOB SOURCE_FILES *.cpp) +file (GLOB HEADER_FILES *.hpp) + +init_target (sip_client) + +build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES}) + +link_boost () +final_target () + diff --git a/examples/sip_client/README.txt b/examples/sip_client/README.txt new file mode 100644 index 0000000000..ce3271c332 --- /dev/null +++ b/examples/sip_client/README.txt @@ -0,0 +1,22 @@ + + +Checkout the project from git + +At the top level, run cmake: + + cmake -G 'Unix Makefiles' \ + -D BUILD_EXAMPLES=ON \ + -D WEBSOCKETPP_ROOT=/tmp/cm1 \ + -D ENABLE_CPP11=OFF . + +and then make the example: + + make -C examples/sip_client + +Now run it: + + bin/sip_client ws://ws-server:80 + +It has been tested against the repro SIP proxy from reSIProcate + + http://www.resiprocate.org/WebRTC_and_SIP_Over_WebSockets diff --git a/examples/sip_client/SConscript b/examples/sip_client/SConscript new file mode 100644 index 0000000000..1ea8b1c5be --- /dev/null +++ b/examples/sip_client/SConscript @@ -0,0 +1,23 @@ +## Main development example +## + +Import('env') +Import('env_cpp11') +Import('boostlibs') +Import('platform_libs') +Import('polyfill_libs') + +env = env.Clone () +env_cpp11 = env_cpp11.Clone () + +prgs = [] + +# if a C++11 environment is avaliable build using that, otherwise use boost +if env_cpp11.has_key('WSPP_CPP11_ENABLED'): + ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs] + prgs += env_cpp11.Program('sip_client', ["sip_client.cpp"], LIBS = ALL_LIBS) +else: + ALL_LIBS = boostlibs(['system','regex','random'],env) + [platform_libs] + [polyfill_libs] + prgs += env.Program('sip_client', ["sip_client.cpp"], LIBS = ALL_LIBS) + +Return('prgs') diff --git a/examples/sip_client/sip_client.cpp b/examples/sip_client/sip_client.cpp new file mode 100644 index 0000000000..3a4de786f3 --- /dev/null +++ b/examples/sip_client/sip_client.cpp @@ -0,0 +1,88 @@ +#include + +#include + +#include + +#include + +#include + +typedef websocketpp::client client; + +using websocketpp::lib::placeholders::_1; +using websocketpp::lib::placeholders::_2; +using websocketpp::lib::bind; + +// pull out the type of messages sent by our config +typedef websocketpp::config::asio_client::message_type::ptr message_ptr; + +// Create a server endpoint +client sip_client; + + +bool received; + +void on_open(client* c, websocketpp::connection_hdl hdl) { + // now it is safe to use the connection + std::cout << "connection ready" << std::endl; + + received=false; + // Send a SIP OPTIONS message to the server: + 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: \r\nFrom: Alice ;tag=1928301774\r\nCall-ID: a84b4c76e66710\r\nCSeq: 63104 OPTIONS\r\nContact: \r\nAccept: application/sdp\r\nContent-Length: 0\r\n\r\n"; + sip_client.send(hdl, SIP_msg.c_str(), websocketpp::frame::opcode::text); +} + +void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) { + client::connection_ptr con = sip_client.get_con_from_hdl(hdl); + + std::cout << "Received a reply:" << std::endl; + fwrite(msg->get_payload().c_str(), msg->get_payload().size(), 1, stdout); + received=true; +} + +int main(int argc, char* argv[]) { + + std::string uri = "ws://localhost:9001"; + + if (argc == 2) { + uri = argv[1]; + } + + try { + // We expect there to be a lot of errors, so suppress them + sip_client.clear_access_channels(websocketpp::log::alevel::all); + sip_client.clear_error_channels(websocketpp::log::elevel::all); + + // Initialize ASIO + sip_client.init_asio(); + + // Register our handlers + sip_client.set_open_handler(bind(&on_open,&sip_client,::_1)); + sip_client.set_message_handler(bind(&on_message,&sip_client,::_1,::_2)); + + websocketpp::lib::error_code ec; + client::connection_ptr con = sip_client.get_connection(uri, ec); + + // Specify the SIP subprotocol: + con->add_subprotocol("sip"); + + sip_client.connect(con); + + // Start the ASIO io_service run loop + sip_client.run(); + + while(!received) { + boost::this_thread::sleep(boost::posix_time::milliseconds(100)); + } + + std::cout << "done" << std::endl; + + } 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; + } +} diff --git a/readme.md b/readme.md index f4f54e9e16..43a8cf03b8 100644 --- a/readme.md +++ b/readme.md @@ -26,7 +26,7 @@ Major Features * Supports secure WebSockets (TLS), IPv6, and explicit proxies. * Flexible dependency management (C++11 Standard Library or Boost) * Interchangable network transport modules (iostream and Boost Asio) -* Portible, cross platform and architecture design +* Portable, cross platform and architecture design Get Involved ============ @@ -50,4 +50,4 @@ http://groups.google.com/group/websocketpp/ Author ====== -Peter Thorson - websocketpp@zaphoyd.com \ No newline at end of file +Peter Thorson - websocketpp@zaphoyd.com