mirror of
https://github.com/XRPLF/rippled.git
synced 2026-04-29 15:37:57 +00:00
update chat_client to new API
This commit is contained in:
@@ -27,7 +27,9 @@
|
||||
|
||||
#include "chat_client_handler.hpp"
|
||||
|
||||
#include "../../src/roles/client.hpp"
|
||||
#include "../../src/websocketpp.hpp"
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
@@ -35,6 +37,8 @@
|
||||
#include <iostream>
|
||||
|
||||
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();
|
||||
|
||||
@@ -30,27 +30,26 @@
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
|
||||
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":[<participant>,...]}
|
||||
// {"type":"msg","sender":"<sender>","value":"<msg>" }
|
||||
void chat_client_handler::decode_server_msg(const std::string &msg) {
|
||||
|
||||
@@ -40,52 +40,44 @@
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#include "../../src/roles/client.hpp"
|
||||
#include "../../src/websocketpp.hpp"
|
||||
#include "../../src/websocket_connection_handler.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <queue>
|
||||
|
||||
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<unsigned char> &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<std::string> m_participants;
|
||||
std::queue<std::string> m_msg_queue;
|
||||
session_ptr m_session;
|
||||
connection_ptr m_con;
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<chat_client_handler> chat_client_handler_ptr;
|
||||
|
||||
Reference in New Issue
Block a user