lifo / cramp

Real-time web application framework in Ruby
http://cramp.in
MIT License
1.5k stars 121 forks source link

cramp websockets not work on Thin since faye-websocket ~> 0.7.1 #64

Closed mateuszdw closed 10 years ago

mateuszdw commented 10 years ago

Since cramp is depedent of 'faye-websocket', '~> 0.7.1' I have problem with websockets connection. Looks like Thin breaking something On latest version websockets response looks like this:

GET /chat HTTP/1.1
Host: localhost:3001
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: PuTGxkRU5cjY8PZ5Y4+4mQ==
Sec-WebSocket-Version: 13

HTTP/1.1 101 Switching Protocols
Connection: keep-alive
Server: thin 1.6.2 codename Doc Brown

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: itIs6clgPwlktYYxEOu6s2pMNs8=

HTTP/1.1 200 OK
Connection: keep-alive
Server: thin 1.6.2 codename Doc Brown
Content-Type: text/html

{"action":"connected"}
{"action":"disconnected"}

Previous dependency'faye-websocket', '~> 0.4.6' gives

GET /chat HTTP/1.1
Host: localhost:3001
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: 5Iba1zzsP3lHZoR9dvNezQ==
Sec-WebSocket-Version: 13

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: gobdn6TEQiGIjj5vW2Vxtj9uMTE=

{"action":"data"}
{"action":"connected"}

Tested with 1.9.3-p392 and Gemfile dependencies

GEM
  remote: http://rubygems.org/
  specs:
    activesupport (3.2.18)
      i18n (~> 0.6, >= 0.6.4)
      multi_json (~> 1.0)
    async-rack (0.5.1)
      rack (~> 1.1)
    cramp (0.15.3)
      activesupport (~> 3.2.16)
      eventmachine (~> 1.0.3)
      faye-websocket (~> 0.7.1)
      rack (~> 1.5.2)
      thor (~> 0.18.0)
    daemons (1.1.9)
    eventmachine (1.0.3)
    faye-websocket (0.7.3)
      eventmachine (>= 0.12.0)
      websocket-driver (>= 0.3.1)
    http_router (0.11.1)
      rack (>= 1.0.0)
      url_mount (~> 0.2.1)
    i18n (0.6.9)
    multi_json (1.10.1)
    rack (1.5.2)
    thin (1.6.2)
      daemons (>= 1.0.9)
      eventmachine (>= 1.0.0)
      rack (>= 1.0.0)
    thor (0.18.1)
    unicode_utils (1.4.0)
    url_mount (0.2.1)
      rack
    websocket-driver (0.3.3)
    yajl-ruby (1.2.0)

PLATFORMS
  ruby

DEPENDENCIES
  async-rack
  cramp
  http_router
  thin
  unicode_utils
  yajl-ruby

So simple app in cramp does not work in current version

class WsAction < Cramp::Websocket
  on_start :on_start
  on_finish :on_finish
  on_data :on_data

  def on_start
  end
  def on_finish
  end
  def on_data
  end
end
mateuszdw commented 10 years ago

this issue can be related with commit https://github.com/faye/faye-websocket-ruby/commit/799103fd47a1d061c864e105993c7f513fa39b45

chall8908 commented 10 years ago

I recently ran across this problem myself and isolated the problem to Cramp's send_initial_response method. It was erroneously sending additional responses after a WebSocket connection had been negotiated by Faye, thus displaying the errors you were seeing. I have an open PR that appears to fix the problem.