diff --git a/examples/chat_client/chat_client.cpp b/examples/chat_client/chat_client.cpp index a2b3952c54..72bb9f3ced 100644 --- a/examples/chat_client/chat_client.cpp +++ b/examples/chat_client/chat_client.cpp @@ -27,7 +27,9 @@ #include "chat_client_handler.hpp" +#include "../../src/roles/client.hpp" #include "../../src/websocketpp.hpp" + #include #include #include @@ -35,6 +37,8 @@ #include using boost::asio::ip::tcp; +using websocketpp::client; + using namespace websocketchat; int main(int argc, char* argv[]) { @@ -45,28 +49,32 @@ int main(int argc, char* argv[]) { } else { uri = argv[1]; } - - chat_client_handler_ptr c(new chat_client_handler()); - + try { - boost::asio::io_service io_service; + chat_client_handler_ptr handler(new chat_client_handler()); + client::connection_ptr con; + client endpoint(handler); - websocketpp::client_ptr client(new websocketpp::client(io_service,c)); + endpoint.alog().unset_level(websocketpp::log::alevel::ALL); + endpoint.elog().unset_level(websocketpp::log::elevel::ALL); - client->init(); + endpoint.elog().set_level(websocketpp::log::elevel::RERROR); + endpoint.elog().set_level(websocketpp::log::elevel::FATAL); + + con = endpoint.get_connection(uri); + + con->add_request_header("User Agent","WebSocket++/0.2.0 WebSocket++Chat/0.2.0"); + con->add_subprotocol("com.zaphoyd.websocketpp.chat"); + + con->set_origin("http://zaphoyd.com"); - client->set_header("User Agent","WebSocket++/2011-09-25"); - client->add_subprotocol("com.zaphoyd.websocketpp.chat"); + endpoint.connect(con); - client->set_origin("http://zaphoyd.com"); - - client->connect(uri); - - boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service)); + boost::thread t(boost::bind(&client::run, &endpoint)); char line[512]; while (std::cin.getline(line, 512)) { - c->send(line); + handler->send(line); } t.join(); diff --git a/examples/chat_client/chat_client_handler.cpp b/examples/chat_client/chat_client_handler.cpp index 8b890a9a13..27ef6bf000 100644 --- a/examples/chat_client/chat_client_handler.cpp +++ b/examples/chat_client/chat_client_handler.cpp @@ -30,27 +30,26 @@ #include using websocketchat::chat_client_handler; -using websocketpp::client_session_ptr; +using websocketpp::client; -void chat_client_handler::on_open(session_ptr s) { - // not sure if anything needs to happen here. - m_session = s; +void chat_client_handler::on_fail(connection_ptr con) { + std::cout << "Connection failed" << std::endl; +} + +void chat_client_handler::on_open(connection_ptr con) { + m_con = con; std::cout << "Successfully connected" << std::endl; } -void chat_client_handler::on_close(session_ptr s) { - // not sure if anything needs to happen here either. - - m_session = client_session_ptr(); +void chat_client_handler::on_close(connection_ptr con) { + m_con = connection_ptr(); std::cout << "client was disconnected" << std::endl; } -void chat_client_handler::on_message(session_ptr s,const std::string &msg) { - //std::cout << "message from server: " << msg << std::endl; - - decode_server_msg(msg); +void chat_client_handler::on_message(connection_ptr con,message_ptr msg) { + decode_server_msg(msg->get_payload()); } // CLIENT API @@ -58,47 +57,31 @@ void chat_client_handler::on_message(session_ptr s,const std::string &msg) { // they need to be careful to not touch unsyncronized member variables. void chat_client_handler::send(const std::string &msg) { - if (!m_session) { - std::cerr << "Error: no connected session" << std::endl; - return; - } - m_session->io_service().post(boost::bind(&chat_client_handler::do_send, this, msg)); -} - -void chat_client_handler::close() { - if (!m_session) { - std::cerr << "Error: no connected session" << std::endl; - return; - } - m_session->io_service().post(boost::bind(&chat_client_handler::do_close,this)); -} - -// END CLIENT API - -void chat_client_handler::do_send(const std::string &msg) { - if (!m_session) { + if (!m_con) { std::cerr << "Error: no connected session" << std::endl; return; } - // check for local commands if (msg == "/list") { std::cout << "list all participants" << std::endl; } else if (msg == "/close") { - do_close(); + close(); } else { - m_session->send(msg); + m_con->send(msg); } } -void chat_client_handler::do_close() { - if (!m_session) { +void chat_client_handler::close() { + if (!m_con) { std::cerr << "Error: no connected session" << std::endl; return; } - m_session->close(websocketpp::session::CLOSE_STATUS_GOING_AWAY,""); + m_con->close(websocketpp::close::status::GOING_AWAY,""); } +// END CLIENT API + + // {"type":"participants","value":[,...]} // {"type":"msg","sender":"","value":"" } void chat_client_handler::decode_server_msg(const std::string &msg) { diff --git a/examples/chat_client/chat_client_handler.hpp b/examples/chat_client/chat_client_handler.hpp index e9e966afec..c13d3f06ba 100644 --- a/examples/chat_client/chat_client_handler.hpp +++ b/examples/chat_client/chat_client_handler.hpp @@ -40,52 +40,44 @@ #include +#include "../../src/roles/client.hpp" #include "../../src/websocketpp.hpp" -#include "../../src/websocket_connection_handler.hpp" #include #include #include -using websocketpp::session_ptr; +using websocketpp::client; namespace websocketchat { -class chat_client_handler : public websocketpp::connection_handler { +class chat_client_handler : public client::handler { public: chat_client_handler() {} virtual ~chat_client_handler() {} - // ignored for clients? - void validate(session_ptr s) {} + void on_fail(connection_ptr con); // connection to chat room complete - void on_open(session_ptr s); + void on_open(connection_ptr con); // connection to chat room closed - void on_close(session_ptr s); + void on_close(connection_ptr con); // got a new message from server - void on_message(session_ptr s,const std::string &msg); - - // ignore messages - void on_message(session_ptr s,const std::vector &data) {} + void on_message(connection_ptr con, message_ptr msg); // CLIENT API void send(const std::string &msg); void close(); private: - // Client API internal - void do_send(const std::string &msg); - void do_close(); - void decode_server_msg(const std::string &msg); // list of other chat participants std::set m_participants; std::queue m_msg_queue; - session_ptr m_session; + connection_ptr m_con; }; typedef boost::shared_ptr chat_client_handler_ptr;