/
close.ex
38 lines (30 loc) · 929 Bytes
/
close.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
defmodule Chaperon.Action.WebSocket.Close do
@moduledoc """
WebSocket connection action that can be run to close the WebSocket connection
with the given `options`.
Closes & removes the `websocket` and `websocket_url` assigned values in
a session when run.
"""
defstruct options: []
end
defimpl Chaperon.Actionable, for: Chaperon.Action.WebSocket.Close do
alias Chaperon.Session
alias Chaperon.Action.WebSocket
use Chaperon.Session.Logging
def run(action, session) do
{ws_conn, ws_url} = WebSocket.for_action(session, action)
WebSocket.Client.close(ws_conn)
session
|> log_info("WS_CLOSE #{ws_url}")
|> WebSocket.delete_for_action(action)
|> Session.ok()
end
def abort(action, session) do
{:ok, action, session}
end
end
defimpl String.Chars, for: Chaperon.Action.WebSocket.Close do
def to_string(%{options: options}) do
"WS Close#{inspect(options)}"
end
end