DEV Community

loading...

Discussion on: HELP NEEDED: Understanding Rails ActionController::Live Module (and Async Limitations)

Collapse
isalevine profile image
Isa Levine Author

Hi Ely, great call on this! response.read_body is exactly what I was missing. I really appreciate you pointing me to the right place in the docs! :)

In the end, here's the listener_controller code I used:

require 'net/http'

class ListenerController < ApplicationController
    def index
        url = URI.parse('http://localhost:3000/broadcaster')

        Net::HTTP.start(url.host, url.port) do |http|
            request = Net::HTTP::Get.new(url.to_s)
            http.request(request) do |response|

                puts <<-READOUT

                res.read_body:
                ==============================

                READOUT

                response.read_body do |data_str|
                    if data_str != ""
                        data_hash = eval(data_str.slice!(6..-1))    # slice to remove leading "data: " substring
                        char_hash = data_hash[:character]
                        Character.create("uuid": char_hash[:uuid], "name": char_hash[:name], "hp": char_hash[:hp], "magic": char_hash[:magic])
                    end
                end
            end
        end
    end
end

Thank you again!