Elixir stream to all subscribers

我只是一个虾纸丫 提交于 2019-12-02 10:13:10

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
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!