Sha256: 2ee9b99aee329b099e839f8cd486692064c7ba37f3f758996f294018db41973d

Contents?: true

Size: 1.13 KB

Versions: 14

Compression:

Stored size: 1.13 KB

Contents

require 'faye/websocket'
require 'permessage_deflate'
require 'rack'

static  = Rack::File.new(File.dirname(__FILE__))
options = {:extensions => [PermessageDeflate], :ping => 5}

App = lambda do |env|
  if Faye::WebSocket.websocket?(env)
    ws = Faye::WebSocket.new(env, ['irc', 'xmpp'], options)
    p [:open, ws.url, ws.version, ws.protocol]

    ws.onmessage = lambda do |event|
      ws.send(event.data)
    end

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

    ws.rack_response

  elsif Faye::EventSource.eventsource?(env)
    es   = Faye::EventSource.new(env)
    time = es.last_event_id.to_i

    p [:open, es.url, es.last_event_id]

    loop = EM.add_periodic_timer(2) do
      time += 1
      es.send("Time: #{time}")
      EM.add_timer(1) do
        es.send('Update!!', :event => 'update', :id => time) if es
      end
    end

    es.send("Welcome!\n\nThis is an EventSource server.")

    es.onclose = lambda do |event|
      EM.cancel_timer(loop)
      p [:close, es.url]
      es = nil
    end

    es.rack_response

  else
    static.call(env)
  end
end

def App.log(message)
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
faye-websocket-0.10.8 examples/app.rb
moonrelay-0.2.1 lib/moonrelay/faye_server.rb
moonrelay-0.2.0 lib/moonrelay/faye_server.rb
faye-websocket-0.10.7 examples/app.rb
faye-websocket-0.10.6 examples/app.rb
faye-websocket-0.10.5 examples/app.rb
faye-websocket-0.10.4 examples/app.rb
faye-websocket-0.10.3 examples/app.rb
faye-websocket-0.10.2 examples/app.rb
faye-websocket-0.10.1 examples/app.rb
faye-websocket-0.10.0 examples/app.rb
faye-websocket-0.9.2 examples/app.rb
faye-websocket-0.9.1 examples/app.rb
faye-websocket-0.9.0 examples/app.rb