質問

faye-websocketというgemを試しているのですが、表記のエラーが出て解消できません.

https://github.com/faye/faye-websocket-ruby

環境

  • ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]
  • faye-websocket (0.9.1)

再現コード

app.rb

require 'faye/websocket'
Faye::WebSocket.load_adapter('thin')
App = lambda do |env|
  if Faye::WebSocket.websocket?(env)
    p 'connected'
    ws = Faye::WebSocket.new(env, nil, ping: 1)

    ws.on :open do
      p 'open'
    end

    ws.on :message do |event|
      p 'got message'
      p event.data
      ws.send(event.data)
    end

    ws.on :close do |event|
      p [:close, event.code, event.reason]
    end

    ws.rack_response
  else
    p 'not faye'
    [200, {'Content-Type' => 'text/plain'}, ['Hello']]
  end
end

config.ru

require './app'

run App

client.rb

require 'faye/websocket'
require 'eventmachine'

EM.run {
  p 'creating Faye Client'
  ws = Faye::WebSocket::Client.new('ws://localhost:9292/')

  ws.on :open do |event|
    p [:open]
    ws.send('Hello, world!!')
    p 'send message'
  end

  ws.on :message do |event|
    p 'got message'
    p [:message, event.data]
  end

  ws.on :close do |event|
    p 'close'
    p [:close, event.code, event.reason]
    ws = nil
  end
}

app.rbのログ

`> rackup                                                               
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on 0.0.0.0:9292, CTRL+C to stop
"connected"
Rack::Lint::LintError: Status must be >=100 seen as integer
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/lint.rb:20:in `assert'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/lint.rb:555:in `check_status'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/lint.rb:51:in `_call'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/lint.rb:37:in `call'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/showexceptions.rb:24:in `call'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/commonlogger.rb:33:in `call'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/chunked.rb:43:in `call'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/content_length.rb:14:in `call'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/connection.rb:86:in `block in pre_process'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/connection.rb:84:in `catch'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/connection.rb:84:in `pre_process'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/connection.rb:53:in `process'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/faye-websocket-0.9.1/lib/faye/adapters/thin.rb:40:in `process'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/connection.rb:39:in `receive_data'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/faye-websocket-0.9.1/lib/faye/adapters/thin.rb:44:in `receive_data'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/backends/base.rb:73:in `start'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/server.rb:162:in `start'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/handler/thin.rb:16:in `run'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/server.rb:264:in `start'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/server.rb:141:in `start'
    /Users/seventh/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/bin/rackup:4:in `<top (required)>'
    /Users/seventh/.rvm/gems/ruby-2.1.5/bin/rackup:23:in `load'
    /Users/seventh/.rvm/gems/ruby-2.1.5/bin/rackup:23:in `<main>'
    /Users/seventh/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `eval'
    /Users/seventh/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `<main>'
127.0.0.1 - - [20/Dec/2014 12:38:11] "GET / HTTP/1.1" 500 120389 0.0364
"open"
"got message"
"Hello, world!!"
[:close, 1002, "One or more reserved bits are on: reserved1 = 1, reserved2 = 0, reserved3 = 0"]`

"Rack::Lint::LintError: Status must be >=100 seen as integer"のエラーは、ws.rack_responseが[-1, {}, []]を返しているからで、今回の相談とは関係なさそうでした.

client.rbのログ

`> ruby client.rb
"creating Faye Client"
[:open]
"send message"
"close"
[:close, 1002, "One or more reserved bits are on: reserved1 = 1, reserved2 = 0, reserved3 = 0"]`

イベントはopen、クライアント側のmessage送信、サーバ側のメッセージ受信までうまくいっていそうで、サーバ側のws.send(event.data)でエラーが起きてcloseしてしまってるようでした. また、WebSocketの仕組みはよく理解できていません.