|
|
|
|
#ifndef _TR_WS_HPP_
|
|
|
|
|
#define _TR_WS_HPP_
|
|
|
|
|
|
|
|
|
|
#include <boost/system/error_code.hpp>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <util.hpp>
|
|
|
|
|
|
|
|
|
|
namespace tr {
|
|
|
|
|
namespace ws {
|
|
|
|
|
|
|
|
|
|
namespace status {
|
|
|
|
|
const int normal = 1000;
|
|
|
|
|
const int unexpected = 1011;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class Session, class Connection>
|
|
|
|
|
void on_error(Session &session, Connection con, const boost::system::error_code &ec) {
|
|
|
|
|
std::cerr << ec.message() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class Session, class Connection>
|
|
|
|
|
void on_close(Session &session, Connection con, int status, const std::string &reason) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class Server, class Session, class Connection>
|
|
|
|
|
void on_open(Server &server, Session &session, Connection con) {
|
|
|
|
|
if (!session.is_valid()) {
|
|
|
|
|
server.send_close(con, status::unexpected, "Session not valid");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class Session, class Connection, class Message>
|
|
|
|
|
void on_message(Session &session, Connection con, Message message) {
|
|
|
|
|
nlohmann::json obj = util::json::parse(*message);
|
|
|
|
|
if (!obj.is_null() && obj.is_object()) {
|
|
|
|
|
auto type = util::json::get("type", obj, std::string());
|
|
|
|
|
if (!type.empty()) {
|
|
|
|
|
if (type == "POST_TORRENT_UPDATES") {
|
|
|
|
|
session.post_torrent_updates();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|