Trying to use Faye with Rack app.

Server:

Faye::WebSocket.load_adapter 'thin'

app = Rack::Builder.new do
  use Rack::CommonLogger
  use Rack::ShowExceptions
  use Faye::RackAdapter, mount: '/ws', timeout: 5 do |bayeux|
    bayeux.on(:subscribe) {|id, channel| p "subscribe #{id} #{channel}"}
    bayeux.on(:publish) {|id, channel, data| p "publish #{id} #{channel} 
#{data}"}
  end

  run ->(env) do
    if Faye::WebSocket.websocket?(env)
      ws = Faye::WebSocket.new(env)

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

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

      # Return async Rack response
      ws.rack_response

    else
      return FREST::HTTP.normal_web(
        context: context,
        env:     env
      )
    end
  end
end

Rack::Handler.get('thin').run app, Port: 8080
client:

(function(){
  window.onload = () => {
    var js = document.createElement("script")
    js.src = "/ws/client.js"
    js.onload = () => {
      document.client = new Faye.Client('http://localhost:8080/ws')

      document.client.subscribe('/test', (m) => {
        console.log(m)
      })
        .then(console.log("Subscribed!"))
    }

    document.head.appendChild(js)
 }
})()

>From browser console:
c = document.client
c.subscribe('/test', (m) => {document.body = m}).then(() => 
{console.log('Then!')}, (e) => {console.log("Error!" + e.message)})
Error!Unknown client
Possibly noteworthy:
- every time I do anything with the client, I have a different client_id
- if I monitor handshake, that gets called repeatedly multiple times per 
second, although it’s not from the line in my onload.
- I’ve tried setting RACK_ENV=production but this does not help

Digging into the Unknown Client in the Faye source code, it appears to arise 
because the client_id sent with the request is not recognized. So I think it’s 
something like I’m closing and reopening the connection repeatedly for some 
reason.
Maybe?
Dunno. Any thoughts?

-- 
-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
--- 
You received this message because you are subscribed to the Google Groups "SD 
Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to