Hello, It is my first development. I want to close connection using the 1006 websocket status code with ‘no_ping’ reason when a timeout occurs. Following the documentation I did not get the expected result.
In my websocket.ex
, I have a code like that:
defmodule MyApp.Websocket do
@behaviour Phoenix.Socket.Transport
def child_spec(_opts) do
:ignore
end
def connect(state) do
{:ok, state}
end
def init(state) do
{:ok, state}
end
def handle_in({input, _opts}, state) do
{:reply, :ok, {:text, "hi"}, state}
end
def handle_info(:timeout, state) do
IO.puts("timeout")
{:stop, :normal, {1006, "no_ping"}, state}
end
def handle_info({:send, channel, message}, state) do
{:push, {:text, "hi"}, state}
end
def terminate(reason, state) do
:ok
end
end
In my endpoint.ex
, I have the timeout defined to 10 seconds:
defmodule MyApp.Endpoint do
use Phoenix.Endpoint, otp_app: :myapp
@session_options [
store: :cookie,
key: "test",
signing_salt: "test",
same_site: "Lax"
]
socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
socket "/v1", MyApp.Websocket,
websocket: [timeout: 10_000, path: "/ws", connect_info: [:peer_data, :x_headers]],
longpoll: false
plug Plug.Static,
at: "/",
from: :myapp,
gzip: false,
only: MyApp.static_paths()
if code_reloading? do
plug Phoenix.CodeReloader
end
plug Phoenix.LiveDashboard.RequestLogger,
param_key: "request_logger",
cookie_key: "request_logger"
plug Plug.RequestId
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library()
plug Plug.MethodOverride
plug Plug.Head
plug Plug.Session, @session_options
plug MyApp.Router
end
As explained on documentation, I expect that a timeout should go into my handle_info(:timeout, state), prints “timeout” and return a 1006 “no_ping” as disconnection reason, but when I connect using wscat:
wscat -c ws://localhost:4000/v1/ws
and wait 10 seconds, the response is:
Disconnected (code: 1000, reason: “”)
What I am doing wrong? I would like to receive a:
Disconnected (code: 1006, reason: “no_ping”)