程序员

利用faye-rails搭建嵌入式faye server

2014-05-06  本文已影响1395人  LinuxNerd

faye是一个实时推送Server,在继续之前建议先了解一下faye。可以单独部署faye server也可以嵌入到rails部署,目前看来嵌入到rails中,是一种能够比较方便监控subscribe、publish、unsubscribe的方式。

首先在Gemfile里增加:

gem 'faye-rails'

运行

bundle install

打开config/application.rb,加入以下内容

require File.expand_path('../../app/realtime/client_event.rb', __FILE__)

module MyApp
  class Application < Rails::Application
  ...
  # faye server
  config.middleware.use FayeRails::Middleware, mount: '/faye', timeout: 25 do
    map '/notify/**' => NotifyController
    map default: NotifyController

    add_extension(ClientEvent.new)
  end
  config.middleware.delete Rack::Lock
end

这里需要一些解释

ClientEvent 类的内容:

# app/realtime/client_event.rb
class ClientEvent
  def incoming(message, callback)
    if message['channel'] !~ %r{^/meta/}
      if message['data']
        if message['data']['token'] != Settings.faye_token
          # Setting any 'error' against the message causes Faye to not accept it.
          message['error'] = "403::Authentication required"
        else
          message['data'].delete('token')
        end
      end
    end
    callback.call(message)
  end
end

对收到的消息,从配置里读取Settings.faye_token匹配一下,做简单的验证,避免其他人直接使用curl发起消息。

接下来看一下比较关键的NotifyController类:

# app/realtime/notify_controller.rb
class NotifyController < FayeRails::Controller
  channel '/notify/**' do
    monitor :subscribe do
      puts "Client #{client_id} subscribed to #{channel}"
    end
    monitor :unsubscribe do
      puts "Client #{client_id} unsubscribed from #{channel}"
    end
    monitor :publish do
      puts "Client #{client_id} published #{data.inspect} to #{channel}"
    end
  end
end

NotifyController继承于FayeRails提供的Controller,匹配到channel后,目前只是输出日志而已。可以在这里进一步的做记录数据库等动作,监控用户的在线状态以及发送消息数等功能。

除了monitor方法来监控订阅发布动作意外,FayeRails还提供了observefilterobserve监控模型的变化,filter对消息进入、发出进行监控。详细看官方README

现在你就可以使用curl命令测试是否成功了(假设token为123456):

curl http://localhost:3000/faye -d 'message={"channel":"/notify/123456", "data": {"content": "hello", "token": "123456" } }'

然后在终端界面上会显示:

Client  published {"content"=>"hello"} to /notify/123456

如果是订阅/notify/123456channel的客户端的话会有一个client_id显示在Client后面。这里因为是用curl命令发送的,因此也没有显示。

好了,在客户端上,或者在服务端上可以轻松地对post请求进行简单的封装,就可以发送实时推送消息给Web端了。

上一篇 下一篇

猜你喜欢

热点阅读