I am implementing a websocket purely using elixir and cowboy. How can i close a websocket connection in the websocket_init/1
function? I want to send a reason to close the connection and then pattern match it in the terminate/3
function, so that i can do some other functions inside it.
def websocket_init(state) do
info("Initializing connection")
case Account.get_user(state.id) do
%User{} ->
{:ok, state}
nil ->
{:error, "No account"}
end
end
So that in the terminate/3
function I can do something like this:
def terminate({:error, "No account"}, _req, _state) do
info("No account")
:ok
end
def terminate(_reason, _req, _state) do
info("Terminating connection")
:ok
end