Skip to content

Detect client disconnection on Server-Sent-Event Server #1370

@gh4ck3r

Description

@gh4ck3r

I'm making server functionality of Server-Sent-Event on cpp-httplib for my project.
I aware there's example code (ssesvr.cc) in the repository and that's where I began for my own implementation.

In my requirement for the functionality, SSE server should keep client as long as possible even there's nothing to send. For that my implementation dedicate a thread per client and keeps it as long as connected. Problem is server can't aware unexpected disconnection of client without trying to send some data. If that kind of connection is increased, it'll consume entire threadpool after all. I know SSE comment can be used for it. But, I don't want to use it to check connectivity because it makes bunch of useless data to be transferred.

So, I made my own patch to tackle the problem and run existing unittest on Linux. But, it makes failure on ServerTest.ClientStop testcase at following.

EXPECT_TRUE(sink.is_writable());

I think the EXPECT_TRUE above should be reconsidered. Because, a sink passed into content provider could be unwritable for some reason if it's based on SocketStream.
I modified detail::SocketStream::is_writable() in the patch to check socket via is_socket_alive(). I think, the patch should be applied to SSLSocketStream as well. I want to contribute the patch with relative unittest and patch if you're OK with changing EXPECT_TRUE above and behavior of SocketStream and SSLSocketStream.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions