Long Polling in Mochiweb - How to tell if client aborts request?
Question
I have a basic mochiweb polling loop that looks like the following, except it does other things instead of printing to the console and eventually returns:
blah() ->
io:format("Blah")
blah()
loop(Req) ->
PathParts = string:tokens(Req:get(path), "/")
case PathParts of
["poll"] ->
blah()
This works great until the client aborts their request. For instance if the client window is closed, this process keeps running indefinitely.
I would like to know if there is an option in mochiweb's start() or maybe something else I'm overlooking that would have mochiweb automatically terminate this process, or at least send a message on client abort. Any ideas?
Solution
Looks like one solution is setting up another process to repeatedly call gen_tcp:recv(Req:get(socket), 0, 1)
and looking for the result {error, closed}
, then killing the polling process if it is received... Not sure if that's optimal though.