问题
I'm trying to implement a radio server in Elixir
One process is always working and reading a file (mp3) and publish to topic ":radio", currently for test purpose when it finishes it starts over
Each connection subscribes to topic ":radio"
I don't understand how to send the chunks to all subscribed connections, the connection closed after 2 or 3 chunks
defmodule Plugtest do
import Plug.Conn
def init(opts), do: opts
def start() do
Plug.Adapters.Cowboy.http(Plugtest, [])
{:ok, _pid} = PubSub.start_link()
spawn(fn -> stream_from_file("./song.mp3", 128) end)
end
def call(conn, _opts) do
conn = conn
|> send_chunked(200)
|> put_resp_content_type("audio/mpeg")
:ok = PubSub.subscribe(spawn(fn -> send_chunk_to_connection(conn) end), :radio)
# File.stream!("./song.mp3", [], 128) |> Enum.into(conn) # test purpose only
end
defp send_chunk_to_connection(conn) do
receive do
{:radio_data, data} ->
IO.inspect "* #{inspect self()} * [ #{inspect conn.owner} ] [ #{inspect data} ]"
# Enum.into(data, conn) # not working TODO send chunk to connection
{:ok, conn} = chunk(conn, data)
send_chunk_to_connection(conn)
end
end
defp stream_from_file(fpath, bytes) do
File.stream!(fpath, [], bytes)
|> Enum.each(fn chunk ->
PubSub.publish(:radio, {:radio_data, chunk})
end)
stream_from_file(fpath, bytes)
end
end
Stacktrace :
[error] Process #PID<0.274.0> raised an exception
** (MatchError) no match of right hand side value: {:error, :closed}
(plugtest) lib/plugtest.ex:26: Plugtest.send_chunk_to_connection/1
dependencies :
defp deps do
[{:plug, "~> 1.0"}, {:cowboy, "~> 1.0"}, {:pubsub, "~> 0.0.2"}]
end
edit after @maxdec comment
defmodule Plugtest do
import Plug.Conn
@file_path "./song.mp3"
@port 4000
@chunk_size 128
def init(opts), do: opts
def start() do
Plug.Adapters.Cowboy.http Plugtest, [], port: @port
{:ok, _pid} = PubSub.start_link()
spawn fn ->
stream_from_file(@file_path, @chunk_size)
end
end
def call(conn, _opts) do
conn = conn
|> send_chunked(200)
|> put_resp_content_type("audio/mpeg")
:ok = PubSub.subscribe(spawn(fn -> send_chunk_to_connection(conn) end), :radio)
# File.stream!("./song.mp3", [], 128) |> Enum.into(conn) # test purpose only
conn
end
defp send_chunk_to_connection(conn) do
receive do
{:radio_data, data} ->
case chunk(conn, data) do
{:ok, conn} -> send_chunk_to_connection(conn)
{:error, err} -> IO.puts err # do nothing, as something went wrong (client disconnection or something else...)
end
end
end
defp stream_from_file(fpath, bytes) do
File.stream!(fpath, [], bytes)
|> Enum.each(fn chunk ->
PubSub.publish(:radio, {:radio_data, chunk})
end)
stream_from_file(fpath, bytes)
end
end
回答1:
After a quick look I think there are 2 things you should fix:
PlugTest
is aPlug
socall/2
should returnconn
(that's not your issue though). It should also blocks while waiting for events (receive
):def call(conn, _opts) do conn = conn |> send_chunked(200) |> put_resp_content_type("audio/mpeg") :ok = PubSub.subscribe(self(), :radio) send_chunk_to_connection(conn) end
In
send_chunk_to_connection
you should do:defp send_chunk_to_connection(conn) do receive do {:radio_data, data} -> case chunk(conn, data) do {:ok, conn} -> send_chunk_to_connection(conn) {:error, err} -> IO.puts err; conn # do nothing, as something went wrong (client disconnection or something else...) end end end
来源:https://stackoverflow.com/questions/38551798/elixir-stream-to-all-subscribers