Skip to content
Open
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
16 changes: 15 additions & 1 deletion assets/js/phoenix/longpoll.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ let arrayBufferToBase64 = (buffer) => {
return btoa(binary)
}

let base64ToArrayBuffer = (base64) => {
let binary = atob(base64)
let len = binary.length
let bytes = new Uint8Array(len)
for(let i = 0; i < len; i++){ bytes[i] = binary.charCodeAt(i) }
return bytes.buffer
}

// Text messages are always JSON, while binary messages are base64 encoded
// by the server, the same way we send them in batchSend.
let decodeMessage = (msg) => {
return msg[0] === "[" || msg[0] === "{" ? msg : base64ToArrayBuffer(msg)
}

export default class LongPoll {

constructor(endPoint, protocols){
Expand Down Expand Up @@ -119,7 +133,7 @@ export default class LongPoll {
// handler disconnects, which cancels the remaining messages.
const timer = setTimeout(() => {
this.messageTimers.delete(timer)
this.onmessage({data: msg})
this.onmessage({data: decodeMessage(msg)})
}, 0)
this.messageTimers.add(timer)
})
Expand Down
20 changes: 15 additions & 5 deletions assets/test/longpoll_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,30 @@ describe("LongPoll", () => {
afterEach(() => jest.useRealTimers())

it("delivers each message in its own task", () => {
Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["first", "second"]})
Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["[\"first\"]", "[\"second\"]"]})
expect(events).toEqual([])

jest.advanceTimersByTime(0)

expect(events).toEqual(["first", "second"])
expect(events).toEqual(["[\"first\"]", "[\"second\"]"])
})

it("delivers base64 encoded messages as binary", () => {
Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["[\"text\"]", "/wEC", "{}"]})
jest.advanceTimersByTime(0)

expect(events[0]).toEqual("[\"text\"]")
expect(events[1]).toBeInstanceOf(ArrayBuffer)
expect(Array.from(new Uint8Array(events[1]))).toEqual([255, 1, 2])
expect(events[2]).toEqual("{}")
})

it("does not deliver messages after a POST timeout closed the transport", () => {
longpoll.send("push")
jest.advanceTimersByTime(0)
const post = Ajax.request.mock.calls.find(([method]) => method === "POST")

Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["message"]})
Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["[\"message\"]"]})
post[5]()
jest.advanceTimersByTime(0)

Expand All @@ -276,10 +286,10 @@ describe("LongPoll", () => {
longpoll.close()
}

Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["first", "second"]})
Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["[\"first\"]", "[\"second\"]"]})
jest.advanceTimersByTime(0)

expect(events).toEqual(["first", "close"])
expect(events).toEqual(["[\"first\"]", "close"])
})
})

Expand Down
20 changes: 13 additions & 7 deletions lib/phoenix/transports/long_poll_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ defmodule Phoenix.Transports.LongPoll.Server do
%{handler: {handler, handler_state}} = state

case handler.handle_in({body, opcode: opcode}, handler_state) do
{:reply, status, {_, reply}, handler_state} ->
{:reply, status, {opcode, reply}, handler_state} ->
state = %{state | handler: {handler, handler_state}}
status = if status == :ok, do: :ok, else: :error
broadcast_from!(state, client_ref, {status, ref})
publish_reply(state, reply)
publish_reply(state, opcode, reply)

{:ok, handler_state} ->
state = %{state | handler: {handler, handler_state}}
Expand Down Expand Up @@ -106,9 +106,9 @@ defmodule Phoenix.Transports.LongPoll.Server do
%{handler: {handler, handler_state}} = state

case handler.handle_info(message, handler_state) do
{:push, {_, reply}, handler_state} ->
{:push, {opcode, reply}, handler_state} ->
state = %{state | handler: {handler, handler_state}}
publish_reply(state, reply)
publish_reply(state, opcode, reply)

{:ok, handler_state} ->
state = %{state | handler: {handler, handler_state}}
Expand All @@ -132,16 +132,22 @@ defmodule Phoenix.Transports.LongPoll.Server do
defp broadcast_from!(_state, client_ref, msg) when is_pid(client_ref),
do: send(client_ref, msg)

defp publish_reply(state, reply) when is_map(reply) do
# Messages are sent to the client inside a JSON list, so binary
# messages are base64 encoded, the same way the client sends them.
defp publish_reply(state, :binary, reply) do
publish_reply(state, :text, reply |> IO.iodata_to_binary() |> Base.encode64())
end

defp publish_reply(state, _opcode, reply) when is_map(reply) do
IO.warn(
"Returning a map from the LongPolling serializer is deprecated. " <>
"Please return JSON encoded data instead (see Phoenix.Socket.Serializer)"
)

publish_reply(state, Phoenix.json_library().encode_to_iodata!(reply))
publish_reply(state, :text, Phoenix.json_library().encode_to_iodata!(reply))
end

defp publish_reply(state, reply) do
defp publish_reply(state, _opcode, reply) do
notify_client_now_available(state)
{:noreply, update_in(state.buffer, &[IO.iodata_to_binary(reply) | &1])}
end
Expand Down
44 changes: 43 additions & 1 deletion test/phoenix/integration/long_poll_channels_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ defmodule Phoenix.Integration.LongPollChannelsTest do
{:noreply, socket}
end

def handle_in("echo_bin", {:binary, bin}, socket) do
push(socket, "bin_push", {:binary, bin})
{:reply, {:ok, {:binary, bin}}, socket}
end

def handle_in("new_msg", message, socket) do
broadcast!(socket, "new_msg", message)
{:noreply, socket}
Expand Down Expand Up @@ -215,6 +220,14 @@ defmodule Phoenix.Integration.LongPollChannelsTest do
decode_body(serializer, resp)
end

defp poll_messages(path, vsn, session, count, acc \\ []) do
resp = poll(:get, path, vsn, session)
assert resp.body["status"] == 200
acc = acc ++ resp.body["messages"]

if length(acc) < count, do: poll_messages(path, vsn, session, count, acc), else: acc
end

defp serializer("2." <> _, json), do: {V2.JSONSerializer, json}
defp serializer(_, nil), do: {V1.JSONSerializer, nil}

Expand All @@ -231,7 +244,11 @@ defmodule Phoenix.Integration.LongPollChannelsTest do
|> update_in([:body], &Phoenix.json_library().decode!(&1))
|> update_in([:body, "messages"], fn messages ->
for msg <- messages || [] do
serializer.decode!(msg, opcode: :text)
case msg do
"[" <> _ -> serializer.decode!(msg, opcode: :text)
"{" <> _ -> serializer.decode!(msg, opcode: :text)
base64 -> {:binary, Base.decode64!(base64)}
end
end
end)
end
Expand Down Expand Up @@ -753,6 +770,31 @@ defmodule Phoenix.Integration.LongPollChannelsTest do
] = resp.body["messages"]
end

test "#{@mode}: pushes and replies with binary messages" do
vsn = "2.0.0"
session = join("/ws", "room:lobby", vsn, "1", @mode)

resp =
poll(:post, "/ws", vsn, session, [
%{
"topic" => "room:lobby",
"event" => "echo_bin",
"join_ref" => "1",
"ref" => "2",
"payload" => {:binary, <<255, 1, 2>>}
}
])

assert resp.body["status"] == 200

# The push and the reply may arrive in different polls
assert [_phx_reply, _user_entered, _joined, {:binary, push}, {:binary, reply}] =
poll_messages("/ws", vsn, session, 5)

assert push == <<0, 1, 10, 8, "1", "room:lobby", "bin_push", 255, 1, 2>>
assert reply == <<1, 1, 1, 10, 2, "1", "2", "room:lobby", "ok", 255, 1, 2>>
end

test "#{@mode}: shuts down after timeout" do
session = join("/ws", "room:lobby", @vsn, "1", @mode)

Expand Down