swisspol/GCDWebServer

How to tell when a client closes/aborts a connection.

Closed this issue · 2 comments

This is somewhat related to issue #171.

In this case however, I'm trying to figure out when a client closes a connection when I'm using an async/async (streamed) set of handlers.

I've got a case where when getting a request on my GCDWebServer, I fire off a long running process that returns data back asynchronously. I add an async handler using:

webServer.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self, asyncProcessBlock: { request, completionBlock in

and then when the handler is called, I created a GCDWebServerStreamedResponse later.

let gcdWebServerResponse = GCDWebServerStreamedResponse(contentType: contentType, asyncStreamBlock: { bodyCompletionBlock in

And hang onto the bodyCompletionBlock so I can use it later.

In the callback from my long running process, I call bodyCompletionBlock(data, nil)with whatever data. However, there's no way for me to know if the GCDWebServer client has closed the connection (which they often do). And thus my long running process keeps going. I know that no data is being transferred, but I want to tell my long running process to stop when the GCDWebServer client closes the connection.

Thanks!

Subclass GCDWebServerConnection so you know when the connection is closed then signal your background process to stop.

@swisspol Could you please provide some more information about subclassing GCDWebServerConnection?

I have a similar case to @SuperTango. I've added a request handler to GCDWebServer object and handling requests works perfectly fine. Now I would like to cancel the connection instantiated in GCDWebServer object when source of this request cancel it.