本文介绍了Rails 应用程序中的 Plezi 路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 plezi 添加到 Gemfile 中并创建了这样的初始化程序:

I've added plezi into Gemfile and created such initializer:

#config/initializer/plezi.rb
class NewsPublisher
  def on_open
    binding.pry
  end
end

route '/ws', NewsPublisher

当我尝试使用此代码通过 JS 连接它时 new WebSocket("ws://127.0.0.1:3000/ws"); 我在客户端收到以下错误消息:WebSocket 握手时出错:意外响应代码:404

When I try to connect it via JS with this code new WebSocket("ws://127.0.0.1:3000/ws"); I receive following error message on client side: Error during WebSocket handshake: Unexpected response code: 404

在服务器端我有:ActionController::RoutingError (No route matching [GET] "/ws"):

谁能解释一下如何在 Rails 应用中使用 plezi 路由?

Can anybody explain how to use plezi routes in Rails app?

推荐答案

在 Plezi 的 入门指南,它指出:

In Plezi's getting started guide, it states that:

如果您的控制器类定义了 on_message(data) 回调,plezi 将自动为该路由启用 websocket 连接.

这个声明可能应该在 websockets 和 Rails 指南中重复......但我仍在编写文档并且它不完整.

This statement should probably be repeated in the websockets and Rails guides... but I'm still writing the documentation and it isn't complete.

要确保 Plezi 为 '/ws' 路由启用 websocket,请添加一个 on_message 回调(甚至是空回调).即:

To make sure Plezi enables websockets for the '/ws' route, add an on_message callback (even an empty one). i.e.:

#config/initializer/plezi.rb
class NewsPublisher
  def on_open
    binding.pry
  end
  def on_message data
    puts "Websocket got: #{data}"
  end
end

Plezi.route '/ws', NewsPublisher

如果您有任何问题,请告诉我.

Please let me know if you have any issues.

编辑(从评论中添加更多信息)

EDIT (adding more information from the comments)

Plezi 使用 Iodine 作为服务器,绕过 Rack 的 hijack API(Iodine 是 Ruby 的快速 C 扩展,它充当机架服务器,具有原生 websockets 支持).

Plezi uses Iodine as it's server, bypassing Rack's hijack API (Iodine is a fast C extension for Ruby, it acts as a Rack server, with native websockets support).

在 Plezi 的 Plezi with Rails/Sinatra/Rack 指南 中指出,在 Rails/Sinatra/Rack 中使用 Plezi 时:

In Plezi's guide to Plezi with Rails/Sinatra/Rack, it is stated that, when using Plezi in Rails/Sinatra/Rack:

只有一个问题 - 每个应用程序不能有多个网络服务器.这意味着我们现有的 Rack 应用程序必须使用 Plezi(实际上是 Iodine)作为它的网络服务器.

确保从 Gemfile 中删除所有服务器(即 thinpumaunicorn 等).

Make sure to remove any servers from the Gemfile (i.e. thin, puma, unicorn etc').

Apache(或 Nginx)和Passenger呢?

Apache 可以将请求转发到 Iodine,很像代理,类似于 Apache 与 puma、passenger、unicorn、thin 和其他应用服务器协同工作的方式.

Apache can forward the requests to Iodine, much like a Proxy and similar to how Apache works together with puma, passenger, unicorn, thin and other app-servers.

可以使用 Apache 或 Nginx,但不能乘客.

It's possible to use Apache or Nginx, but not passenger.

性能?

测试并查看.

Iodine 是一个 C 扩展,它使用 epollkqueue 系统调用 - 它们非常快并且专为并发而构建.此外,Iod​​ine 具有非常好的内存管理设计,其中传递给Ruby-land"的 Ruby 对象经常被回收或清除以优化性能……但这只是字面上的意思.只有一种方法可以知道——测试.

Iodine is a C extension that utilizes either epoll or kqueue system calls - which are very fast and built for concurrency. Also, Iodine has a very good memory management design, where Ruby objects passed to "Ruby-land" are often recycled or cleared to allow for optimized performance... but that's all just word. There is only one way to know - test.

这篇关于Rails 应用程序中的 Plezi 路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 14:57