【问题标题】:How to test LiteCable broadcasting from irb console?如何从 irb 控制台测试 LiteCable 广播?
【发布时间】:2020-09-11 07:39:04
【问题描述】:

其实我的问题不仅仅是控制台。我还没有找到任何用于测试LiteCable 的库,我尝试实现一些。我现在面临的问题是,当我在控制台中调用类似(与官方example完全相同)时:

LiteCable.broadcast "chat_#{chat_id}", user: user, message: data["message"], sid: sid

它返回零。我尝试将byebug 放入代码中,当我在那里使用完全相同的参数调用此方法时,我得到了带有一些不可读参数的相当大的表格。主要问题是我没有看到我尝试从控制台发送的消息。

有人能解释一下为什么会这样吗?如何实现从控制台发送请求?关键是我正在开发仅 api 的应用程序,并尝试通过适当的测试来涵盖此功能。非常感谢您!

P.S:该项目是在纯 ruby​​ 上,没有 rails

【问题讨论】:

  • 如果您使用的是纯 Ruby,没有 Rails,为什么要使用 ActionCable?使用 ActionCable 的结果是您要么受限于 hijack 逻辑,要么需要将 WebSocket 连接转发到不同的服务器,就像 AnyCable 一样。 LiteCable 也是如此...I blogged about this once upon a time,此后变化不大。
  • 你的广播适配器设置了什么?
  • @theterminalguy 默认一个。没有明确设置

标签: ruby websocket actioncable


【解决方案1】:

由于您没有完整的代码,我无法清楚地了解您的设置。 LiteCable 的默认broadcast_adaptermemory,因此如果您要从控制台广播,控制台外的进程将不会收到此类流。如果你使用anycable adapter,我相信它在redis中排队广播,那么只要你有正确的连接,你应该能够从你的控制台广播。

这里是一个简单的 WS 客户端和一个使用没有 Rails 的 LiteCable 实现的服务器

# ~/app/server.rb
# A simple WS server that evaluates math expression literally using ruby eval
# Run this server with ruby server.rb

require "rack"
require "rack/handler/puma"
require "lite_cable"
require "lite_cable/server"

class CalculatorConnection < LiteCable::Connection::Base
  identified_by :token

  def connect
    self.token = request.params["token"]

    $stdout.puts "Session #{token} is connected to the server"
  end

  def disconnect
    $stdout.puts "Session #{token} disconnected from the server"
  end
end

class CalculatorChannel < LiteCable::Channel::Base
  identifier :calculator
  
  def subscribed
    stream_from "calculator"
  end

  def evaluate(payload)
    $stdout.puts "Data received from the client"

    LiteCable.broadcast "calculator", result: eval(payload['expression']) 
  end
end

app = Rack::Builder.new 
app.map "/cable" do
  use LiteCable::Server::Middleware, connection_class: CalculatorConnection
  
  run(proc { |_| [200, {"Content-Type" => "text/plain"}, ["OK"]] })
end

Rack::Handler::Puma.run app

// ~/app/client.js
// A client that talks to the WS server.
// To run, Open chrome console and paste this code 

let socket = null;

(function init() {
  socket = new WebSocket("ws://0.0.0.0:9292/cable?token=abc123");

  socket.onopen = function (e) {
    console.info("[connected] : Connection established to the server");
  };

  socket.onmessage = function (e) {
    console.info("[message] : Message from the server", e.data);
  };

  socket.onerror = function (e) {
    console.error("[error] : Error establishing connection", e.message);
  };

})();

// subscribe to the calculator channel
socket.send(
  JSON.stringify({
    command: "subscribe",
    identifier: JSON.stringify({
      channel: "calculator",
    }),
  })
);

// call the evaluate method on the Calculator channel
socket.send(
  JSON.stringify({
    command: "message",
    identifier: JSON.stringify({
      channel: "calculator",
    }),
    data: JSON.stringify({
      expression: "150 * 4 - 3 / (2 + 2)",
      action: "evaluate",
    }),
  })
);

这里没有什么特别之处,只是一个使用 Puma 的简单机架应用程序,它增加了对机架套接字劫持的支持。这应该会引导您朝着正确的方向前进。确保您的服务器和 chrome 控制台都保持打开状态以查看输出。尝试向服务器发送其他消息以查看输出。

如果您正在考虑在 ruby​​ 中而不是使用 LiteCable 的 js 中实现 WebSocket 客户端,请在 LiteCable 存储库中查看check the client socket class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 2013-12-18
    • 2012-05-03
    • 1970-01-01
    • 2018-11-11
    相关资源
    最近更新 更多