Sha256: e64e53500bbf10542a97f8ca312f8698086e447dbc3d47c950ad7d63929c2cee

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

#!ruby -I ../../lib -I lib
require 'frankie'
require 'faye/websocket'

#
# Open localhost:9000/public/index.html in the browser
#

Faye::WebSocket.load_adapter('thin')

class WebSockets
  def initialize app=nil, opts={}, &blk
    @app = app
    @path = opts.fetch :path, '/'
    @blk = blk
  end

  def call env
    return @app.call(env) unless env['PATH_INFO'] == @path

    if Faye::WebSocket.websocket?(env)
      ws = Faye::WebSocket.new(env)

      if @blk
        Proc.new(&@blk).call ws
      else
        handle ws
      end

      ws.rack_response
    else
      @app.call(env)
    end
  end

  def handle ws
  end
end

class App < Frankie::App
  #Serve static assets from public folder
  use Rack::Static, :urls => ["/public"]

  use WebSockets, :path => '/websocket' do |ws|
    ws.on :message do |event|
      ws.send(event.data)
    end

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

  get '/frankie' do
    'yep, you can still use frankie'
  end
end

App.run! 9000

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
nyny-1.0.0.pre1 examples/web_sockets/server.rb
little_frankie-1.0.0.pre1 examples/web_sockets/server.rb