Skip to content

Commit

Permalink
Improve the new interface of barrage (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
lanytcc authored Jul 21, 2023
1 parent 4add201 commit be85288
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
12 changes: 8 additions & 4 deletions wiliwili/include/api/live/danmaku_live.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

//
// Created by maye174 on 2023/4/6.
//
Expand Down Expand Up @@ -26,26 +25,31 @@ class LiveDanmaku : public brls::Singleton<LiveDanmaku> {
int uid;
void connect(int room_id, int uid);
void disconnect();
bool is_connected();

void send_join_request(int room_id, int uid);

void send_heartbeat();
void send_text_message(const std::string &message);

void setonMessage(std::function<void(std::string)> func);
std::function<void(std::string)> onMessage;

void set_wait_time(int time);
int wait_time = 600;

LiveDanmaku();
~LiveDanmaku();

bool is_connected();
std::atomic_bool connected{false};
bool is_evOK();
std::atomic_bool ms_ev_ok{false};

std::thread mongoose_thread;
std::thread heartbeat_thread;
std::mutex mongoose_mutex;
mg_mgr *mgr;
mg_connection *nc;

//人气值
int popularity = 0;
};
};
29 changes: 17 additions & 12 deletions wiliwili/source/api/danmaku_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ LiveDanmaku::LiveDanmaku() {
printf("WSAStartup failed with error: %d\n", result);
}
#endif
heartbeat_thread = std::thread([this]() {
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(20));
if (this->is_connected() and this->is_evOK()) {
this->send_heartbeat();
}
}
});
}

LiveDanmaku::~LiveDanmaku() {
Expand Down Expand Up @@ -74,24 +82,13 @@ void LiveDanmaku::connect(int room_id, int uid) {

// Start Mongoose event loop and heartbeat thread
mongoose_thread = std::thread([this]() {
int last = 0;
int s = 0;
while (this->is_connected()) {
this->mongoose_mutex.lock();
if(this->nc == nullptr) {
break;
}
this->mongoose_mutex.unlock();
mg_mgr_poll(this->mgr, 800);
s += 1;
if (s - last >= 36) {
this->send_heartbeat();
last = s;
}
if (s < 0) {
s = 0;
last = 0;
}
mg_mgr_poll(this->mgr, wait_time);
}
mg_mgr_free(this->mgr);
delete this->mgr;
Expand All @@ -114,10 +111,18 @@ void LiveDanmaku::disconnect() {
}
}

void LiveDanmaku::set_wait_time(int time){
wait_time = time;
}

bool LiveDanmaku::is_connected() {
return connected.load(std::memory_order_acquire);
}

bool LiveDanmaku::is_evOK(){
return ms_ev_ok.load(std::memory_order_acquire);
}

void LiveDanmaku::send_join_request(int room_id, int uid) {
json join_request = {
{"clientver", "1.6.3"},
Expand Down

0 comments on commit be85288

Please sign in to comment.