这个article描述了一种在routes.rb中分享Sinatra应用和Rails应用之间会话的简单方法:
MyRailsApp::Application.routes.draw do
mount MySinatraApp.new => '/api'
end
要创建我的Sinatra应用程序,我遵循了tutorial,其中介绍了如何创建Sinatra WebSockets示例。
与上面的方法类似,这是我在routes.rb中尝试的:
MyRailsApp::Application.routes.draw do
mount ChatDemo::ChatBackend.new => '/api'
end
但是我看到了这个错误:
! Unable to load application: ArgumentError: wrong number of arguments (0 for 1)
/sinatra_rails_test2/config/application.rb:22:in `initialize': wrong number of arguments (0 for 1) (ArgumentError)
看起来应用程序必须作为参数传递。我如何在routes.rb文件中执行此操作?这是正确的解决方案吗?
作为参考,这是/lib/chat_app.rb中初始化方法的样子:
require 'faye/websocket'
require 'thread'
require 'redis'
require 'json'
require 'erb'
module ChatDemo
class ChatBackend
KEEPALIVE_TIME = 15 # in seconds
CHANNEL = "chat-demo"
def initialize(app)
@app = app
@clients = []
uri = URI.parse(ENV["REDISCLOUD_URL"])
@redis = Redis.new(host: uri.host, port: uri.port, password: uri.password)
Thread.new do
redis_sub = Redis.new(host: uri.host, port: uri.port, password: uri.password)
redis_sub.subscribe(CHANNEL) do |on|
on.message do |channel, msg|
@clients.each {|ws| ws.send(msg) }
end
end
end
end
# the module continues...