我希望流式传输从证券交易所(通过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"
})
答案 0 :(得分:0)
嗯......我希望这(至少)帮助其他人。
我意识到我的问题是
StockPriceChannel.server.broadcast_to("portfolio_#
{params[:portfolio_id]}",quote)
应该是
ActionCable.server.broadcast_to("portfolio_#
{params[:portfolio_id]}",quote)