|
This should have been done a *long* time ago, back when I initially
added Websocket support. This is the first part of two in improving
loop handler support with regards to socket closure.
Reason may include: {normal, shutdown} for the most normal shutdown,
{normal, timeout} for a loop handler timeout shutdown, or {error, _}
if an error occured.
|
|
Now init/3 can return one of the following values to enable loops:
- {loop, Req, State}
- {loop, Req, State, hibernate}
- {loop, Req, State, Timeout}
- {loop, Req, State, Timeout, hibernate}
Returning one of these tuples will activate looping in the HTTP handler.
When looping, handle/2 is never called. Instead, Cowboy will listen
for Erlang messages and forward them to the info/3 function of the
handler. If a timeout is defined, Cowboy will also close the connection
when no message has been received for Timeout milliseconds.
The info/3 function is defined as info(Msg, Req, State). It can return
either of the following tuples:
- {ok, Req, State}
- {loop, Req, State}
- {loop, Req, State, hibernate}
The first one ends the connection, calling terminate/2 before closing.
The others continue the loop.
Loops are useful when writing long-polling handlers that need to wait
and don't expect to receive anything. Therefore it is recommended to
set a timeout to close the connection if nothing arrives after a while
and to enable hibernate everywhere.
Normal HTTP handlers shouldn't need to use this and as such info/3
was made optional.
|