代码拉取完成,页面将自动刷新
同步操作将从 王祥/im 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include"mongoose.h"
#include<iostream>
#include<string>
#include"util.hpp"
namespace im{
class Server{
private:
SessionManager *ss_manager;
Usertable *user_table;
Message *message_table;
struct mg_mgr _mgr;
std::unordered_map<struct mg_connection*,uint64_t> conn_ss;
std::string _root = "./www.root";
private:
static void user_add(struct mg_connection *c,struct mg_http_message *hm,Server *ser)
{
std::string body(hm->body.ptr,hm->body.len);
Json::Value user_json;
bool ret = JsonUtil::unserialize(body, &user_json);
if(ret == false)
{
Json::Value err;
err["result"] = "false";
err["reason"] = "解析出错";
std::string body1;
JsonUtil::serialize(err,&body1);
mg_http_reply(c,400,NULL,"%s",body1.c_str());
return ;
}
Usertable *table = ser->get_usertable();
ret = table->insert(user_json);
if(ret == false)
{
Json::Value err;
err["result"] = "false";
err["reason"] = "插入失败";
std::string body1;
JsonUtil::serialize(err,&body1);
mg_http_reply(c,500,NULL,"%s",body1.c_str());
return ;
}
Json::Value res;
res["result"] = "true";
res["reason"] = "插入成功";
std::string body1;
JsonUtil::serialize(res,&body1);
mg_http_reply(c,200,NULL,"%s",body1.c_str());
}
static void user_login(struct mg_connection *c,struct mg_http_message *hm,Server *ser)
{
std::string body(hm->body.ptr,hm->body.len);
Json::Value user_json;
bool ret = JsonUtil::unserialize(body, &user_json);
if(ret == false)
{
Json::Value err;
err["result"] = "false";
err["reason"] = "解析出错";
std::string body1;
JsonUtil::serialize(err,&body1);
mg_http_reply(c,400,NULL,"%s",body1.c_str());
return ;
}
Usertable *table = ser->get_usertable();
ret = table->check_login(user_json);
if(ret == false)
{
Json::Value err;
err["result"] = "false";
err["reason"] = "登录失败";
std::string body1;
JsonUtil::serialize(err,&body1);
mg_http_reply(c,401,NULL,"%s",body1.c_str());
return ;
}
SessionManager *ssm = ser->get_session();
std::unordered_map<struct mg_connection*,uint64_t> *connss = ser->get_conn_ss();
uint64_t id = ssm->insert(user_json);
// connss->insert(std::make_pair<c,id>);
(*connss)[c] = id;
std::string cookie = "Set-Cookie: SSID=" + std::to_string(id)+"\r\n";
Json::Value res;
res["result"] = "true";
res["reason"] = "登录成功";
std::string body1;
JsonUtil::serialize(res,&body1);
mg_http_reply(c,200,cookie.c_str(),"%s",body1.c_str());
}
static void get_hostroy_message(struct mg_connection *c,struct mg_http_message *hm,Server *ser)
{
Json::Value user_json;
Message *msg = ser->get_messagetable();
msg->select(3000,&user_json);
std::string body;
JsonUtil::serialize(user_json,&body);
mg_http_reply(c,200,"Content-Type:application/json\r\n","%s",body.c_str());
}
static void boradcast_msg(struct mg_mgr *mgr,const std::string &msg)
{
struct mg_connection *conn = mgr->conns;
for(;conn != NULL;conn = conn->next)
{
if(conn->is_websocket)
{
mg_ws_send(conn,msg.c_str(),msg.size(),WEBSOCKET_OP_TEXT);
}
}
}
static void handler(struct mg_connection *c,int ev,void *ev_date ,void *data)
{
Server *ser = (Server*)data;
if(ev == MG_EV_HTTP_MSG)
{
struct mg_http_message *hm = (struct mg_http_message *)ev_date;
std::string method(hm->method.ptr,hm->method.len);
std::string uri(hm->uri.ptr,hm->uri.len);
if(method == "POST" && uri == "/user")//xin zeng
{
// std::cout<<"--------";
user_add(c,hm,ser);
}
else if(method == "POST" && uri == "/login")//deng lu
{
user_login(c,hm,ser);
}
else if(method == "PUT" && uri == "/user/password")//xiu gai mi ma
{
}
else if(method == "PUT" && uri == "/user/nickname")//ciu gai ni cheng
{
}
else if(method == "DELDTE" && uri == "/user")//zhu xiao yong hu
{
}
else if(method == "GET" && uri == "/user")//huo qu yong hu
{
struct mg_str *cookie = mg_http_get_header(hm,"Cookie");
if(cookie == NULL)
{
std::cout<<"--------";
mg_http_reply(c,401,"","%s","cookie-------401");
return ;
}
std::string cookie_str(cookie->ptr,cookie->len);
std::cout<<cookie_str<<std::endl;
size_t pos = cookie_str.find("=");
std::string key = cookie_str.substr(pos+1);
uint64_t ssid = std::atoi(key.c_str());
std::cout<<ssid<<std::endl;
SessionManager *ssm = ser->get_session();
if(ssm->exist(ssid) == false)
{
std::cout<<"-------------------";
mg_http_reply(c,401,"","%s","ssid----------401");
return ;
}
Json::Value user;
ssm->get_session_id(ssid,&user);
Usertable *table = ser->get_usertable();
Json::Value new_user;
table->select_id(user["username"].asString(),&new_user);
std::string user_str;
JsonUtil::serialize(new_user,&user_str);
mg_http_reply(c,200,"Content-Type: application/json\r\n","%s",user_str.c_str());
}
else if(method == "GET" && uri == "/message")
{
get_hostroy_message(c,hm,ser);
}
else if(method == "GET"&& uri == "/ws")//xie yi qie huan
{
std::cout<<"ws1"<<std::endl;
struct mg_str *cookie = mg_http_get_header(hm,"Cookie");
if(cookie == NULL)
{
mg_http_reply(c,401,"","%s","401");
return ;
}
std::string cookie_str(cookie->ptr,cookie->len);
size_t pos = cookie_str.find("=");
std::string key = cookie_str.substr(pos+1);
uint64_t ssid = std::atoi(key.c_str());
SessionManager *ssm = ser->get_session();
if(ssm->exist(ssid)== false)
{
mg_http_reply(c,401,"","%s","401");
return ;
}
std::cout<<"ws"<<std::endl;
mg_ws_upgrade(c,hm,NULL);
}
else{
struct mg_http_serve_opts opt = {.root_dir = ser->get_root().c_str()};
mg_http_serve_dir(c,hm,&opt);
}
}
else if(ev == MG_EV_WS_OPEN )
{
//wo shou cheng gong
}
else if(ev == MG_EV_WS_MSG)
{
//ke hu duo fa song xiao xi
struct mg_ws_message *wsmsg = (struct mg_ws_message*)ev_date;
std::string msg(wsmsg->data.ptr,wsmsg->data.len);
std::cout<<msg<<std::endl;
boradcast_msg(ser->get_mgr(),msg);
Json::Value json;
JsonUtil::unserialize(msg,&json);
ser->get_messagetable()->insert(json);
}
else if(ev == MG_EV_CLOSE)
{
//lian jie duo kai
}
}
public:
Server()
{
mg_mgr_init(&_mgr);
user_table = new Usertable();
message_table = new Message();
ss_manager = new SessionManager();
}
void start(int port)
{
std::string address = "0.0.0.0:" + std::to_string(port);
mg_http_listen(&_mgr,address.c_str(),handler,this);
for(;;)mg_mgr_poll(&_mgr,1000);
}
struct mg_mgr* get_mgr(){return &_mgr;}
std::string& get_root(){return _root;}
Usertable* get_usertable(){return user_table;}
Message* get_messagetable(){return message_table;}
SessionManager* get_session(){return ss_manager;}
std::unordered_map<struct mg_connection*,uint64_t>* get_conn_ss(){return &conn_ss;}
};
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。