Elixir流向所有订阅者

时间:2016-07-24 12:04:02

标签: streaming elixir cowboy

我正在尝试在Elixir中实现无线电服务器

一个进程始终正在工作并读取文件(mp3)并发布到主题“:radio”,目前用于测试目的,当它完成时重新开始

每个连接都订阅主题“:radio”

我不明白如何将块发送到所有订阅的连接,连接在2或3块之后关闭

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

依赖项:

  defp deps do
    [{:plug, "~> 1.0"}, {:cowboy, "~> 1.0"}, {:pubsub, "~> 0.0.2"}]
  end

在@maxdec评论之后编辑

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 个答案:

答案 0 :(得分:1)

快速浏览后,我认为应该解决两件事:

  1. PlugTestPlug,因此call/2应该返回conn(但这不是您的问题)。它也应该在等待事件时阻塞(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
    
  2. send_chunk_to_connection你应该这样做:

    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