Skip to content

Commit bfefcc8

Browse files
committed
Handle remote closed andrewvy#37
1 parent 3de6b9f commit bfefcc8

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/page_session.ex

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ defmodule ChromeRemoteInterface.PageSession do
119119
# ---
120120

121121
def init(url) do
122+
Process.flag(:trap_exit, true)
122123
{:ok, socket} = ChromeRemoteInterface.Websocket.start_link(url)
123124

124125
state = %__MODULE__{
@@ -282,8 +283,17 @@ defmodule ChromeRemoteInterface.PageSession do
282283
|> Enum.each(&send(&1, event))
283284
end
284285

285-
def terminate(_reason, state) do
286-
Process.exit(state.socket, :kill)
286+
# handle the trapped exit call
287+
def handle_info({:EXIT, from, reason}, %{socket: socket} = state) do
288+
cleanup(reason, state)
289+
# see GenServer docs for other return types
290+
{:stop, reason, state}
291+
end
292+
293+
def terminate(reason, state) do
294+
cleanup(reason, state)
287295
:stop
288296
end
297+
298+
def cleanup(reason, state), do: Process.exit(state.socket, reason)
289299
end

lib/websocket.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,13 @@ defmodule ChromeRemoteInterface.Websocket do
1111
send(state, {:message, frame_data})
1212
{:ok, state}
1313
end
14+
15+
def handle_disconnect(_status, state) do
16+
Process.exit(state, :remote_closed)
17+
{:ok, state}
18+
end
19+
20+
def terminate({:remote, :closed}, _state) do
21+
:stop
22+
end
1423
end

0 commit comments

Comments
 (0)