如何在 Rails 中使用 websockets
How to use websockets in Rails
我正在尝试将 Web 套接字集成到我的 Rails 应用程序中 link:https://github.com/websocket-rails/websocket-rails/wiki/Installation-and-Setup
我使用 Ruby 2.0 和 Rails3。
相关文件内容:
宝石文件:
gem 'websocket-rails
events.rb
WebsocketRails::EventMap.describe do
subscribe :awesomeness_approval, :to => ChatController, :with_method => :awesomeness_approval
end
development.rb
...
config.middleware.delete Rack::Lock
...
chat_controller.rb
class ChatController < WebsocketRails::BaseController
def initialize_session
# perform application setup here
controller_store[:message_count] = 0
end
def awesomeness_approval
if message[:awesomeness] > 5
trigger_success {:m => 'awesome level is sufficient'}
else
trigger_failure {:m => 'awesome level is insufficient'}
end
end
end
chat.js
var dispatcher = new WebSocketRails('localhost:3000/websocket');
dispatcher.on_open = function(data) {
console.log('Connection has been established: ', data);
}
var success = function(response) {
console.log("You are awesome because: "+response.message);
}
var failure = function(response) {
console.log("You are not very awesome because: "+response.message);
}
var message = { awesomeness: 4 }
dispatcher.trigger('awesomeness_approval', message, success, failure);
当我尝试启动 rails 服务器时,出现以下错误:
w/app/controllers/chat_controller.rb:9: syntax error, unexpected =>, expecting '}' (SyntaxError)
trigger_success {:m => 'awesome level is sufficient'}
...
/app/controllers/chat_controller.rb:11: syntax error, unexpected =>, expecting '}'
trigger_failure {:m => 'awesome level is insufficient'}
但是,如果我注释掉以上几行,我就可以启动服务器,并且浏览器可以连接到网络套接字。在浏览器控制台上看到以下输出:
"Connection has been established: " Object { connection_id: "f66c4298eb103312e181" } chat.js:3
请帮忙。
您的错误来自方法调用中的语法歧义。
写的时候
trigger_success {:m => 'awesome level is sufficient'}
左大括号被解释为方法块参数的开始。但是,您希望它是一个散列。解释为一个块,它确实是错误的语法,因为块不允许简单的哈希火箭。
你可以解决这个歧义,你可以使用括号:
trigger_success({:m => 'awesome level is sufficient'})
这样,对于 ruby 解析器来说,您实际上是想将单个散列参数传递给该方法并且当前已被解析。
我正在尝试将 Web 套接字集成到我的 Rails 应用程序中 link:https://github.com/websocket-rails/websocket-rails/wiki/Installation-and-Setup
我使用 Ruby 2.0 和 Rails3。 相关文件内容:
宝石文件:
gem 'websocket-rails
events.rb
WebsocketRails::EventMap.describe do
subscribe :awesomeness_approval, :to => ChatController, :with_method => :awesomeness_approval
end
development.rb
... config.middleware.delete Rack::Lock ...
chat_controller.rb
class ChatController < WebsocketRails::BaseController
def initialize_session
# perform application setup here
controller_store[:message_count] = 0
end
def awesomeness_approval
if message[:awesomeness] > 5
trigger_success {:m => 'awesome level is sufficient'}
else
trigger_failure {:m => 'awesome level is insufficient'}
end
end
end
chat.js
var dispatcher = new WebSocketRails('localhost:3000/websocket');
dispatcher.on_open = function(data) {
console.log('Connection has been established: ', data);
}
var success = function(response) {
console.log("You are awesome because: "+response.message);
}
var failure = function(response) {
console.log("You are not very awesome because: "+response.message);
}
var message = { awesomeness: 4 }
dispatcher.trigger('awesomeness_approval', message, success, failure);
当我尝试启动 rails 服务器时,出现以下错误:
w/app/controllers/chat_controller.rb:9: syntax error, unexpected =>, expecting '}' (SyntaxError)
trigger_success {:m => 'awesome level is sufficient'}
...
/app/controllers/chat_controller.rb:11: syntax error, unexpected =>, expecting '}'
trigger_failure {:m => 'awesome level is insufficient'}
但是,如果我注释掉以上几行,我就可以启动服务器,并且浏览器可以连接到网络套接字。在浏览器控制台上看到以下输出:
"Connection has been established: " Object { connection_id: "f66c4298eb103312e181" } chat.js:3
请帮忙。
您的错误来自方法调用中的语法歧义。
写的时候
trigger_success {:m => 'awesome level is sufficient'}
左大括号被解释为方法块参数的开始。但是,您希望它是一个散列。解释为一个块,它确实是错误的语法,因为块不允许简单的哈希火箭。
你可以解决这个歧义,你可以使用括号:
trigger_success({:m => 'awesome level is sufficient'})
这样,对于 ruby 解析器来说,您实际上是想将单个散列参数传递给该方法并且当前已被解析。