我可能会避免使用ActionCable语义和内部协议,其中包括非浏览器客户端的API项目。
例如:
这并不意味着您需要完全离开Rails。在非Rails Websocket替代品中使用Rails模型和代码应该很容易。
此外,Ruby还为ActionCable提供了一些不错的Websocket替代方案。
我很有偏见,是Iodine - an HTTP/Websocket server with native Pub/Sub和Plezi.io, a real-time web application framework的作者......但我可能会使用碘(有或没有Plezi提供的附加舒适度)。
与Plezi一个简单的WebSocket应用程序看起来像这样(严重的是,使用irb
运行从终端下面的代码,它的工作原理):
require 'plezi'
class ChatServer
def index
"Use Websockets to connect."
end
def on_open
@name = params['id'] || "anonymmous"
subscribe channel: "chat"
publish channel: "chat", message: "#{@name} joind the chat."
write "Welcome, #{@name}!"
end
def on_close
publish channel: "chat", message: "#{@name} left the chat."
end
def on_message data
publish channel: "chat", message: "#{@name}: #{data}"
end
def on_shutdown
write "Server shutting down. Goodbye #{@name}"
end
end
Plezi.route '/', ChatServer
# We'll monitor message just for kicks:
subscription = Iodine.subscribe(pattern: "*") do |channel, message|
# print a log?
puts "\n* Message on channel #{channel}:\n#{message}\n"
end
# make sure we don't duplicate our monitoring on every process.
root_pid = Process.pid
Iodine.run { Iodine.unsubscribe(subscription) unless Process.pid == root_pid }
exit
没有Redis的所需的服务器,没有什么特别的东西准备和可以在Rails应用程序中使用plezi作为中间件(运行iodine
服务器而不是puma
)。
我会考虑目标客户端。我不知道ActionCable是否有非浏览器客户端,并且您可能希望Websocket API也可以访问本机移动应用程序(不仅仅是浏览器)....?附:我有偏见,因为我是碘(Ruby web套接字服务器)和[Plezi](http://www.plezi.io)(基于碘构建的实时应用程序框架)的作者。我投我自己的行动,让ActionSable的手中没有websockets。 – Myst
是的,我的主要目标是处理手机,我想知道ActionCable是否适合实现这一点。 – mike927