我偏向于Plezi,它是为缩放(使用Redis)和can be used to easily add websocket support to your existing web-app而构建的...但是再次,我可能不会客观。
在Rails中运行Plezi有两种方式 - 合并应用程序/服务器(使用Iodine HTTP/Websocket server)或使用Redis同步这两个应用程序。
两种方式都很容易设置。
为了您的Rails应用程序中使用Plezi,添加plezi
您Gemfile
,并转移到“薄”或“美洲狮”,或从Gemfile
任何其他服务器的任何引用 - 这应该允许碘自动接管。比将Plezi.app
作为中间件放置在您的应用中。你可以通过要求它的文件包括一个预先制作的Plezi应用程序,或者 - 更简单 - 你可以将代码写入Rails文件之一(可能使用'initializers','helpers'或'models'文件夹)。
尝试添加以下代码聊天室服务器:
require 'plezi'
# do you need automated redis support?
# require 'redis'
# ENV['PL_REDIS_URL'] = "redis://user:[email protected]:6379"
class BroadcastCtrl
def index
# we can use the websocket echo page to test our server,
# just remember to update the server address in the form.
redirect_to 'http://www.websocket.org/echo.html'
end
def on_message data
# try replacing the following two lines are with:
# self.class.broadcast :_send_message, data
broadcast :_send_message, data
response << "sent."
end
def _send_message data
response << data
end
end
route '/broadcast', BroadcastCtrl
这使我们能够注入一些Rails的幻成Plezi有的Plezi魔术Rails的......例如,可以很容易地保存用户的WebSocket UUID并发送更新:
require 'plezi'
# do you need automated redis support?
# require 'redis'
# ENV['PL_REDIS_URL'] = "redis://user:[email protected]:6379"
class UserNotifications
def on_open
get_current_user.websocket_uuid = uuid
get_current_user.save
end
def on_close
# wrap all of the following in a transaction, or scaling might
# create race conditions and corrupt UUID data
return unless UsersController.get_current_user.websocket_uuid == uuid
get_current_user.websocket_uuid = nil
get_current_user.save
end
def on_message data
# get data from user and use it somehow
end
protected
def get_current_user
# # use your authentication system here, maybe:
# @user ||= UserController.auth_user(cookies[:my_session_id])
end
def send_message data
response << data
end
end
route '/', UserNotifications
# and in your UserController
def UserController < ApplicationController
def update
# your logic and than send notification:
data = {}
UserNotifications.unicast @user.websocket_uuid, :send_message, data.to_json
end
end
谢谢。我试图在我的Rails应用程序中使用它,但是我对如何做到这一点感到困惑。有没有我可以看到的一个示例rails应用程序?我也得到这个错误:“试图更新执行权限,这是依赖于系统并可能失败。”你知道那是关于什么吗?它不会让我从plezi安装运行默认的plezi应用程序。 – baron816
消息''试图更新执行权限,这是依赖于系统的,可能失败了“'不是错误。 Plezi总是写这个,因为执行许可更新基于MacOSX,并且应该在类Unix系统上工作,但不能在Windows上工作。这是一个合理的tweek,它允许你从操作系统的GUI以及终端UI运行应用程序....其余的我将在编辑答案时尝试编写。 – Myst
感谢您的支持。我发现只要安装gem会减慢JavaScript的速度,至少在开发模式下是如此。这是正常的吗?我仍然很难把所有东西都放在一起,并且放在一起。我想我可能会延期包括websockets一段时间。 – baron816