Skip to content

Commit 0e0c2d7

Browse files
committed
Add websocket/longpoll macro
1 parent d6a715d commit 0e0c2d7

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

lib/phoenix/socket/router.ex

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ defmodule Phoenix.Socket.Router do
1212
path = Path.join(path, end_segment)
1313

1414
quote do
15-
match :*,
16-
unquote(path),
17-
Phoenix.Transports.WebSocket,
18-
[
19-
{:user_socket, unquote(user_socket)}
20-
| unquote(websocket)
21-
]
15+
websocket unquote(path), unquote(user_socket), unquote(websocket)
2216
end
2317
else
2418
[]
@@ -31,13 +25,7 @@ defmodule Phoenix.Socket.Router do
3125
path = Path.join(path, end_segment)
3226

3327
quote do
34-
match :*,
35-
unquote(path),
36-
Phoenix.Transports.LongPoll,
37-
[
38-
{:user_socket, unquote(user_socket)}
39-
| unquote(longpoll)
40-
]
28+
longpoll unquote(path), unquote(user_socket), unquote(longpoll)
4129
end
4230
else
4331
[]
@@ -49,6 +37,30 @@ defmodule Phoenix.Socket.Router do
4937
end
5038
end
5139

40+
defmacro websocket(path, user_socket, opts \\ []) do
41+
quote do
42+
match :*,
43+
unquote(path),
44+
Phoenix.Transports.WebSocket,
45+
[
46+
{:user_socket, unquote(user_socket)}
47+
| unquote(opts)
48+
]
49+
end
50+
end
51+
52+
defmacro longpoll(path, user_socket, opts \\ []) do
53+
quote do
54+
match :*,
55+
unquote(path),
56+
Phoenix.Transports.LongPoll,
57+
[
58+
{:user_socket, unquote(user_socket)}
59+
| unquote(opts)
60+
]
61+
end
62+
end
63+
5264
defp put_auth_token(true, enabled), do: [auth_token: enabled]
5365
defp put_auth_token(opts, enabled), do: Keyword.put(opts, :auth_token, enabled)
5466
end

test/phoenix/integration/websocket_channels_test.exs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,20 @@ defmodule Phoenix.Integration.WebSocketChannelsTest do
181181
get "/", SetSession, []
182182

183183
scope "/ws" do
184-
socket "/", UserSocket,
185-
websocket: [
184+
# socket "/", UserSocket,
185+
# websocket: [
186+
# check_origin: ["//example.com"],
187+
# timeout: 200,
188+
# error_handler: {UserSocket, :handle_error, []}
189+
# ]
190+
scope "/" do
191+
websocket "/websocket", UserSocket,
186192
check_origin: ["//example.com"],
187193
timeout: 200,
188194
error_handler: {UserSocket, :handle_error, []}
189-
]
195+
196+
longpoll "/longpoll", UserSocket
197+
end
190198

191199
socket "/admin", UserSocket,
192200
websocket: [

test/phoenix/integration/websocket_socket_test.exs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,14 @@ defmodule Phoenix.Integration.WebSocketTest do
9090
use Phoenix.Router
9191
import Phoenix.Socket.Router
9292

93-
# extract stuff from Phoenix.Transports.WebSocket, which
94-
# can now simply be part of the endpoint (Plug.Session, …)
95-
96-
# Manual route definition API
97-
# scope "/ws" do
98-
# match :*, "/websocket", Phoenix.Transports.WebSocket,
99-
# user_socket: UserSocket, check_origin: ["//example.com"], subprotocols: ["sip"], timeout: 200
100-
#
101-
# match :*, "/longpoll", Phoenix.Transports.LongPoll, user_socket: UserSocket
102-
# end
103-
104-
socket "/ws", UserSocket,
105-
websocket: [check_origin: ["//example.com"], subprotocols: ["sip"], timeout: 200]
93+
# socket "/ws", UserSocket,
94+
# websocket: [check_origin: ["//example.com"], subprotocols: ["sip"], timeout: 200]
95+
scope "/ws" do
96+
websocket "/websocket", UserSocket,
97+
check_origin: ["//example.com"], subprotocols: ["sip"], timeout: 200
98+
99+
longpoll "/longpoll", UserSocket
100+
end
106101

107102
socket "/custom/some_path", UserSocket,
108103
websocket: [path: "nested/path", check_origin: ["//example.com"], timeout: 200]

0 commit comments

Comments
 (0)