我正在尝试使用 em-websocket 构建一个 rails websocket 应用程序。我已将以下代码放在文件 config/initializers/websocket.rb 中,但是当我运行“rails server”时,应用程序无法启动。如果我删除代码它开始正常。同样的事情发生在我的本地机器和服务器上。
require 'eventmachine'
require 'em-websocket'
EventMachine.run {
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
ws.onopen {
puts "WebSocket connection open"
ws.send "Hello Client"
}
ws.onclose { puts "Connection closed" }
ws.onmessage { |msg|
puts "Recieved message: #{msg}"
ws.send "Pong: #{msg}"
}
end
puts "Websocket started"
}
我得到这个控制台打印出来
=> Booting WEBrick
=> Rails 3.2.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Websocket started
如果我删除上面的代码,它开始正常,我得到:
=> Booting WEBrick
=> Rails 3.2.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-12-21 04:19:52] INFO WEBrick 1.3.1
[2012-12-21 04:19:52] INFO ruby 1.9.3 (2012-04-20) [i386-mingw32]
[2012-12-21 04:19:52] INFO WEBrick::HTTPServer#start: pid=1484 port=3000
任何想法将不胜感激