em-websocket を使用して Rails Websocket アプリケーションを構築しようとしています。以下のコードをファイル config/inititalizers/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
どんなアイデアでも大歓迎です