Elixir stream to all subscribers

后端 未结 1 1291
一个人的身影
一个人的身影 2021-01-28 19:15

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

1条回答
  •  时光取名叫无心
    2021-01-28 20:01

    After a quick look I think there are 2 things you should fix:

    1. PlugTest is a Plug so call/2 should return conn (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
      
    2. 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
      

    0 讨论(0)
提交回复
热议问题