Sha256: b37a5b22b6b0cab5497d2b08c420c8501c46d912815b8f3117f91365a3546ec4

Contents?: true

Size: 1.04 KB

Versions: 10

Compression:

Stored size: 1.04 KB

Contents

require 'faye/websocket'
require 'rack'

static = Rack::File.new(File.dirname(__FILE__))

App = lambda do |env|
  if Faye::WebSocket.websocket?(env)
    ws = Faye::WebSocket.new(env, ['irc', 'xmpp'], :ping => 5)
    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

10 entries across 10 versions & 2 rubygems

Version Path
faye-websocket-0.7.3 examples/app.rb
faye-tls1-websocket-0.8.0 examples/app.rb
faye-websocket-0.7.2 examples/app.rb
faye-websocket-0.7.1 examples/app.rb
faye-websocket-0.7.0 examples/app.rb
faye-websocket-0.6.3 examples/app.rb
faye-websocket-0.6.2 examples/app.rb
faye-websocket-0.6.1 examples/app.rb
faye-websocket-0.6.0 examples/app.rb
faye-websocket-0.5.0 examples/app.rb