Faye Websocket示例错误?

时间:2015-09-28 02:00:24

标签: ruby websocket faye ruby-2.2

Faye Websocket for Ruby,有一个特定的例子对我不起作用。

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

应用程序确实运行,但条件是:

App = lambda do |env|

未得到满足且未进行迭代。这个文件应该由外部源运行吗?丢失文件?

这是通过命令运行文件:

ruby app.rb

1 个答案:

答案 0 :(得分:0)

我尝试的websocket服务器示例是通过同时拥有'app.rb'和'config.ru'文件来运行的。瘦,faye,permessage_deflate,json,我认为必须使用'gem install'安装bundler才能使用此示例。

运行脚本的命令在'config.ru'中详细说明,在此处发布以供参考

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