mistgc / bili-live-chat

A bilibili streaming chat tool using TUI written in Rust.
24 stars 3 forks source link

[BUG] Can't send message & miss some messages in live channel #3

Open futuretech6 opened 1 year ago

futuretech6 commented 1 year ago

I can send message using bilibili_live_tui with same device and network in the same room but can't use this.

Also, some messages are lost occasionally.

mistgc commented 1 year ago

Yeah, I found this issue. Actually, if you use this to connect a live room that has a lot of audience, this will lose some messages. I will try to fix it in a couple of days.

mistgc commented 1 year ago

I found I can't send danmaku in bilibili_live_tui sometimes but can do it in bili-live-chat.

That's weird!!!

bilibili_live_tui.1:

image

bilibili_live_tui.2:

image

bili-live-chat:

image

mistgc commented 1 year ago

If you modify the following code in src/app.rs, can avoid losing messages occasionally.

let client2 = self.danmu_client.clone();
let recv_msg = tokio::spawn(async move {
    loop {
        client2.lock().await.receive().await;
        // tokio::time::sleep(Duration::from_secs_f32(0.3)).await;
        tokio::time::sleep(Duration::from_secs_f32(0.01)).await;
    }
});
futuretech6 commented 1 year ago

If you modify the following code in src/app.rs, can avoid losing messages occasionally.

let client2 = self.danmu_client.clone();
let recv_msg = tokio::spawn(async move {
    loop {
        client2.lock().await.receive().await;
        // tokio::time::sleep(Duration::from_secs_f32(0.3)).await;
        tokio::time::sleep(Duration::from_secs_f32(0.01)).await;
    }
});

So that's some sync problem?

futuretech6 commented 1 year ago

I send several messages using bilibili_live_tui, none of them fails. But I can't a single message using bili-live-chat, and there is no error message such as "发送弹幕失败".

mistgc commented 1 year ago

If you modify the following code in src/app.rs, can avoid losing messages occasionally.

let client2 = self.danmu_client.clone();
let recv_msg = tokio::spawn(async move {
    loop {
        client2.lock().await.receive().await;
        // tokio::time::sleep(Duration::from_secs_f32(0.3)).await;
        tokio::time::sleep(Duration::from_secs_f32(0.01)).await;
    }
});

So that's some sync problem?

Actually yes, because there are 2 tasks that need to manipulate the client, and the client be protected by Mutex. If we do not send a 'heartbeat' to the server within the 60s, the server would close the connection with us. So we have to sleep the receiving message task and release the Mutex protecting the client to make sure that the 'heartbeat' be sent within a reasonable time.

mistgc commented 1 year ago

And I found a possible reason that some messages can not be sent. That is caused by the data that do not be encoded by urlencode.

mistgc commented 1 year ago

At the branch dev 95402a0f09eb5e2b0e9454c7b332da770cb96cbe, the sync problem had been solved.

futuretech6 commented 1 year ago

At the branch dev 95402a0, the sync problem had been solved.

I test it using roomId 23141761, it is ok but the messages box are refreshed once many seconds rather then fetched immediately (possibly due to the message frequency is very high in that room)

mistgc commented 1 year ago

I found that when messages are sent very frequently, the Danmaku Receiver in the BiLiBiLi web client also has this problem.