Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Socket] send connect error to client and close #4886

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions assets/js/phoenix/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ export default class Socket {

if(this.hasLogger()) this.log("receive", `${payload.status || ""} ${topic} ${event} ${ref && "(" + ref + ")" || ""}`, payload)

if (topic === 'phoenix') {
this.onPhoenixMessage(event, payload, ref, join_ref)
}

for(let i = 0; i < this.channels.length; i++){
const channel = this.channels[i]
if(!channel.isMember(topic, event, payload, join_ref)){ continue }
Expand All @@ -543,6 +547,12 @@ export default class Socket {
})
}

onPhoenixMessage(event, payload, ref, join_ref) {
if (event === 'phx_error') {
this.onConnError(payload)
}
}

leaveOpenTopic(topic){
let dupChannel = this.channels.find(c => c.topic === topic && (c.isJoined() || c.isJoining()))
if(dupChannel){
Expand Down
13 changes: 11 additions & 2 deletions lib/phoenix/endpoint/cowboy2_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ defmodule Phoenix.Endpoint.Cowboy2Handler do

{:cowboy_websocket, copy_resp_headers(conn, req), [handler | state], cowboy_opts}

{:error, %Plug.Conn{adapter: {@connection, req}} = conn} ->
{:ok, copy_resp_headers(conn, req), {handler, opts}}
{:error, reason, %Plug.Conn{adapter: {@connection, req}} = conn} ->
{:cowboy_websocket, copy_resp_headers(conn, req), [{:error, reason}, handler]}
end

{:plug, conn, handler, opts} ->
Expand Down Expand Up @@ -136,6 +136,15 @@ defmodule Phoenix.Endpoint.Cowboy2Handler do

## Websocket callbacks

def websocket_init([{:error, reason}, handler]) do
last_frame = Jason.encode!([nil, nil, "phoenix", "phx_error", reason])
# Close frame payloads have a max size of 123 bytes
{[
{:text, last_frame},
{:close, 3000, ""}
], [handler]}
end

def websocket_init([handler | state]) do
{:ok, state} = handler.init(state)
{:ok, [handler | state]}
Expand Down
5 changes: 2 additions & 3 deletions lib/phoenix/transports/websocket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ defmodule Phoenix.Transports.WebSocket do

case handler.connect(config) do
{:ok, state} -> {:ok, conn, state}
:error -> {:error, Plug.Conn.send_resp(conn, 403, "")}
:error -> {:error, :unknown, conn}
{:error, reason} ->
{m, f, args} = opts[:error_handler]
{:error, apply(m, f, [conn, reason | args])}
{:error, reason, conn}
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions priv/static/phoenix.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions priv/static/phoenix.cjs.js.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions priv/static/phoenix.js
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,9 @@ var Phoenix = (() => {
}
if (this.hasLogger())
this.log("receive", `${payload.status || ""} ${topic} ${event} ${ref && "(" + ref + ")" || ""}`, payload);
if (topic === "phoenix") {
this.onPhoenixMessage(event, payload, ref, join_ref);
}
for (let i = 0; i < this.channels.length; i++) {
const channel = this.channels[i];
if (!channel.isMember(topic, event, payload, join_ref)) {
Expand All @@ -1112,6 +1115,11 @@ var Phoenix = (() => {
}
});
}
onPhoenixMessage(event, payload, ref, join_ref) {
if (event === "phx_error") {
this.onConnError(payload);
}
}
leaveOpenTopic(topic) {
let dupChannel = this.channels.find((c) => c.topic === topic && (c.isJoined() || c.isJoining()));
if (dupChannel) {
Expand Down
2 changes: 1 addition & 1 deletion priv/static/phoenix.min.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions priv/static/phoenix.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,9 @@ var Socket = class {
}
if (this.hasLogger())
this.log("receive", `${payload.status || ""} ${topic} ${event} ${ref && "(" + ref + ")" || ""}`, payload);
if (topic === "phoenix") {
this.onPhoenixMessage(event, payload, ref, join_ref);
}
for (let i = 0; i < this.channels.length; i++) {
const channel = this.channels[i];
if (!channel.isMember(topic, event, payload, join_ref)) {
Expand All @@ -1083,6 +1086,11 @@ var Socket = class {
}
});
}
onPhoenixMessage(event, payload, ref, join_ref) {
if (event === "phx_error") {
this.onConnError(payload);
}
}
leaveOpenTopic(topic) {
let dupChannel = this.channels.find((c) => c.topic === topic && (c.isJoined() || c.isJoining()));
if (dupChannel) {
Expand Down
4 changes: 2 additions & 2 deletions priv/static/phoenix.mjs.map

Large diffs are not rendered by default.