0

Over at Faye Websocket for Ruby, there is a specific example that is not working for me.

https://raw.githubusercontent.com/faye/faye-websocket-ruby/master/examples/app.rb

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

The application does run, however the condition:

App = lambda do |env|

is not being met and is not iterating. Is this file supposed to be ran by an external source? Missing a file?

This is with running the file by command:

ruby app.rb
Myst
  • 18,516
  • 2
  • 45
  • 67
CZauX
  • 109
  • 2
  • 9
  • 1
    Welcome to SO. Here are a few thoughts: 1. `App = lambda ...` is not a condition, it's as assignment; 2. From the Faye website it seems that the example file is part of an example which consists of a number of files... You might be running only part of the example code...; 3. The example seems to be a Rack application, you might consider running it using `rackup` or using whatever server the example is expected to work with (probably `thin` or `puma`). – Myst Sep 28 '15 at 05:22
  • 1
    P.S. You might get more effective information by explaining what it is you are trying to do and what it is that you have tried so far to accomplish that goal. – Myst Sep 28 '15 at 05:25
  • You are correct, config.ru and app.rb are a single example together, has to be ran with one of the supported methods. For me, it detailed the command 'thin start -R examples/config.ru -p 7000', just had to change the directory and everything worked well. Thank you! I'l be posting an issue on their git about it. – CZauX Sep 28 '15 at 06:36

1 Answers1

0

The websocket server example I was trying is run by having both the 'app.rb' and 'config.ru' file. Thin, faye, permessage_deflate, json, and I think bundler have to be installed with 'gem install ' for this example to work.

The command to run the script is detailed in the 'config.ru', posting it here for reference

thin start -R examples/config.ru -p 7000

CZauX
  • 109
  • 2
  • 9