Skip to content

Commit be1d9fd

Browse files
committed
Merge pull request XRPLF#248 from resiprocate/0.3.x-cmake
Fix typo, add sip_client example (SIP over WebSockets)
2 parents 17083a4 + de4ad9e commit be1d9fd

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

examples/sip_client/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
file (GLOB SOURCE_FILES *.cpp)
3+
file (GLOB HEADER_FILES *.hpp)
4+
5+
init_target (sip_client)
6+
7+
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
8+
9+
link_boost ()
10+
final_target ()
11+

examples/sip_client/README.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
3+
Checkout the project from git
4+
5+
At the top level, run cmake:
6+
7+
cmake -G 'Unix Makefiles' \
8+
-D BUILD_EXAMPLES=ON \
9+
-D WEBSOCKETPP_ROOT=/tmp/cm1 \
10+
-D ENABLE_CPP11=OFF .
11+
12+
and then make the example:
13+
14+
make -C examples/sip_client
15+
16+
Now run it:
17+
18+
bin/sip_client ws://ws-server:80
19+
20+
It has been tested against the repro SIP proxy from reSIProcate
21+
22+
http://www.resiprocate.org/WebRTC_and_SIP_Over_WebSockets

examples/sip_client/SConscript

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Main development example
2+
##
3+
4+
Import('env')
5+
Import('env_cpp11')
6+
Import('boostlibs')
7+
Import('platform_libs')
8+
Import('polyfill_libs')
9+
10+
env = env.Clone ()
11+
env_cpp11 = env_cpp11.Clone ()
12+
13+
prgs = []
14+
15+
# if a C++11 environment is avaliable build using that, otherwise use boost
16+
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
17+
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
18+
prgs += env_cpp11.Program('sip_client', ["sip_client.cpp"], LIBS = ALL_LIBS)
19+
else:
20+
ALL_LIBS = boostlibs(['system','regex','random'],env) + [platform_libs] + [polyfill_libs]
21+
prgs += env.Program('sip_client', ["sip_client.cpp"], LIBS = ALL_LIBS)
22+
23+
Return('prgs')

examples/sip_client/sip_client.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <condition_variable>
2+
3+
#include <websocketpp/config/asio_no_tls_client.hpp>
4+
5+
#include <websocketpp/client.hpp>
6+
7+
#include <iostream>
8+
9+
#include <boost/thread/thread.hpp>
10+
11+
typedef websocketpp::client<websocketpp::config::asio_client> client;
12+
13+
using websocketpp::lib::placeholders::_1;
14+
using websocketpp::lib::placeholders::_2;
15+
using websocketpp::lib::bind;
16+
17+
// pull out the type of messages sent by our config
18+
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
19+
20+
// Create a server endpoint
21+
client sip_client;
22+
23+
24+
bool received;
25+
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;
29+
30+
received=false;
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);
34+
}
35+
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);
38+
39+
std::cout << "Received a reply:" << std::endl;
40+
fwrite(msg->get_payload().c_str(), msg->get_payload().size(), 1, stdout);
41+
received=true;
42+
}
43+
44+
int main(int argc, char* argv[]) {
45+
46+
std::string uri = "ws://localhost:9001";
47+
48+
if (argc == 2) {
49+
uri = argv[1];
50+
}
51+
52+
try {
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);
56+
57+
// Initialize ASIO
58+
sip_client.init_asio();
59+
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));
63+
64+
websocketpp::lib::error_code ec;
65+
client::connection_ptr con = sip_client.get_connection(uri, ec);
66+
67+
// Specify the SIP subprotocol:
68+
con->add_subprotocol("sip");
69+
70+
sip_client.connect(con);
71+
72+
// Start the ASIO io_service run loop
73+
sip_client.run();
74+
75+
while(!received) {
76+
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
77+
}
78+
79+
std::cout << "done" << std::endl;
80+
81+
} catch (const std::exception & e) {
82+
std::cout << e.what() << std::endl;
83+
} catch (websocketpp::lib::error_code e) {
84+
std::cout << e.message() << std::endl;
85+
} catch (...) {
86+
std::cout << "other exception" << std::endl;
87+
}
88+
}

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Major Features
2626
* Supports secure WebSockets (TLS), IPv6, and explicit proxies.
2727
* Flexible dependency management (C++11 Standard Library or Boost)
2828
* Interchangable network transport modules (iostream and Boost Asio)
29-
* Portible, cross platform and architecture design
29+
* Portable, cross platform and architecture design
3030

3131
Get Involved
3232
============
@@ -50,4 +50,4 @@ http://groups.google.com/group/websocketpp/
5050

5151
Author
5252
======
53-
Peter Thorson - websocketpp@zaphoyd.com
53+
Peter Thorson - websocketpp@zaphoyd.com

0 commit comments

Comments
 (0)