使用ActionCableClient连接与主应用程序一起记录和接收数据

时间:2019-03-19 15:20:41

标签: ruby eventmachine actioncable

启动应用程序时,我希望ActionCable将日志记录通过恒定连接传输到服务器。

通过这种连接,我想进行交互-按需发送动作和阅读收到的消息。

由于到目前为止我几乎没有处理过EventMachine,并且在Internet上没有发现有关我的问题的信息,因此这里是我的问题。

我将用真实的ruby和ruby伪代码混合描述我的问题/解决方案。

require 'action_cable_client'

my_thread = Thread.new do
  EventMachine.run do
    @@message_storage = []
    uri = "ws://127.0.0.1:3000/cable"
    client = ActionCableClient.new(uri, 'LoggingChannel')

    client.connected do
      puts 'successfully connected.'
    end

    client.disconnected do
      puts 'disconnected.'
    end

    client.received do |message|
      puts "Got a message: #{message}"
      @@message_storage += [message]
    end
  end
end

# here is my main app loop

some_actions()

if error
  my_thread.client.perform('error', {message: 'error 123 occured'})
end

if warning
  my_thread.client.perform('info', {message: 'some information for you'})
end

puts "Currently there are #{my_thread.@@message_storage.size} messages in storage"

有解决方案吗?

其他信息,我的意图是

我没有弄清楚(因此只有伪代码,在逻辑上看起来还不错)

  • 我如何与EventMachine中的ActionCable交互以从外部在其中执行任务
  • 我如何从外部访问EventMachine / ActionCable内部的值

0 个答案:

没有答案