Receive and send messages
This commit is contained in:
46
user.cpp
46
user.cpp
@@ -3,7 +3,6 @@
|
||||
#include <tools.h>
|
||||
#include <json/json.h>
|
||||
#include <unistd.h>
|
||||
#include <future>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include "room.h"
|
||||
@@ -23,7 +22,12 @@ namespace Yc {
|
||||
_stop(false) {
|
||||
_token = Yc::Lib::Tools::generateRandomString(32);
|
||||
sendMsg(token, _token, "", "");
|
||||
std::async(std::bind(&User::checkerTask, this));
|
||||
thread = new std::thread(&User::checkerTask, this);
|
||||
}
|
||||
|
||||
User::~User() {
|
||||
delete thread;
|
||||
_parent->addMessage(User::system, "leaved_chat", _name, _color);
|
||||
}
|
||||
|
||||
std::string User::name() const {
|
||||
@@ -44,11 +48,7 @@ namespace Yc {
|
||||
sendMessage["message"] = message;
|
||||
sendMessage["userName"] = userName;
|
||||
sendMessage["color"] = color;
|
||||
std::string outString;
|
||||
std::stringstream outStream;
|
||||
outStream << sendMessage;
|
||||
outString = outStream.str();
|
||||
write(_socket, outString.c_str(), outString.length());
|
||||
send(sendMessage);
|
||||
}
|
||||
|
||||
void User::sendMsg(User::MsgType type, Json::Value message, std::string userName, std::string color) {
|
||||
@@ -57,11 +57,7 @@ namespace Yc {
|
||||
sendMessage["message"] = message;
|
||||
sendMessage["userName"] = userName;
|
||||
sendMessage["color"] = color;
|
||||
std::string outString;
|
||||
std::stringstream outStream;
|
||||
outStream << sendMessage;
|
||||
outString = outStream.str();
|
||||
write(_socket, outString.c_str(), outString.length());
|
||||
send(sendMessage);
|
||||
}
|
||||
|
||||
void User::checkerTask() {
|
||||
@@ -71,11 +67,19 @@ namespace Yc {
|
||||
FD_SET(_socket, &readSd);
|
||||
timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 100;
|
||||
if (select(_socket + 1, &readSd, NULL, NULL, &tv) < 0 && errno != ETIMEDOUT) {
|
||||
tv.tv_usec = 500;
|
||||
int selectResult = select(_socket + 1, &readSd, NULL, NULL, &tv);
|
||||
std::cout << selectResult << "/" << FD_ISSET(_socket, &readSd) << std::endl;
|
||||
if (selectResult == 1 && FD_ISSET(_socket, &readSd) == 1) {
|
||||
_parent->removeUser(_token);
|
||||
_stop = true;
|
||||
}
|
||||
std::string msg = readSocket(_socket);
|
||||
if (msg == "") {
|
||||
continue;
|
||||
}
|
||||
std::cout << msg << std::endl;
|
||||
handleMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,5 +91,19 @@ namespace Yc {
|
||||
return _color;
|
||||
}
|
||||
|
||||
void User::send(std::string out) {
|
||||
Base::send(_socket, out);
|
||||
}
|
||||
|
||||
void User::send(Json::Value out) {
|
||||
Base::send(_socket, out);
|
||||
}
|
||||
|
||||
void User::handleMessage(std::string message) {
|
||||
Json::Value jsonTree = getJsonTree(message);
|
||||
if (jsonTree["type"].asString() == "message") {
|
||||
_parent->addMessage(User::message, jsonTree["message"].asString(), _name, _color);
|
||||
}
|
||||
}
|
||||
} // namespace Lib
|
||||
} // namespace Yc
|
||||
|
||||
Reference in New Issue
Block a user