【问题标题】:Action Cable (Rails 5) Pulling data from Stock ExchangeAction Cable (Rails 5) 从证券交易所提取数据
【发布时间】:2017-08-29 11:42:38
【问题描述】:

我希望流式传输从证券交易所(通过 websocket)接收到的数据并将该数据广播给客户端。我相信行动电缆是最好的方法,但到目前为止还没有成功。

“DataProvider”在 Ruby 上构建了一个 SDK,用于在我的服务器和证券交易所服务器之间建立连接。我已经能够成功连接,但是没有看到数据流进来。

有什么建议吗?

频道:

class StockPriceChannel < ApplicationCable::Channel
  def subscribed
    stream_from "portfolio_#{params[:portfolio_id]}"

  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
    EventMachine.stop()
  end

  def receive(data)
    tickers = data["message"]

    tickers = tickers.split(",")

    puts tickers


    options = {
       username: ENV["DataProvider_USERNAME"],
       password: ENV["DataProvider_PASSWORD"],
      channels: tickers
     }

    EventMachine.run do

     client = DataProvider::Realtime::Client.new(options)
      client.on_quote do |quote|
        StockPriceChannel.server.broadcast_to("portfolio_#
 {params[:portfolio_id]}",quote)
      end
      client.connect()

      EventMachine.add_timer(1) do
        puts "joining tickers"
        client.join(tickers)
      end

  #     EventMachine.add_timer(10) do
  #       client.disconnect()
  #       EventMachine.stop()
  #     end

    end
  end
end

ReactJS(在 componentDidMount() 中)

App.portfolioChannel = App.cable.subscriptions.create(
  {
    channel:"StockPriceChannel",
    portfolio_id:this.props.match.params.port_id
  },
  {
    connected: () => console.log("StockPriceChannel connected"),
    disconnected: () => console.log("StockPriceChannel disconnected"),
    received: data => {
      debugger;
      console.log(data)
    }
  }
)
App.portfolioChannel.send({
  message:"AAPL,MSFT"
})

【问题讨论】:

    标签: ruby-on-rails reactjs actioncable


    【解决方案1】:

    嗯...我希望这(至少)对其他人有所帮助。

    我意识到我的问题是

    StockPriceChannel.server.broadcast_to("portfolio_#
        {params[:portfolio_id]}",quote)
    

    应该是

    ActionCable.server.broadcast_to("portfolio_#
        {params[:portfolio_id]}",quote)
    

    【讨论】:

      猜你喜欢
      • 2018-03-28
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多