所以我在我的应用程序中使用faye pub-sub,发布是从不同的应用程序发生的,在我的faye.js中我已经为rails编写了ajax post方法。现在,如果我的应用程序的5页在浏览器中打开,faye.js将被加载5次,post方法被调用5次。如果没有打开单个页面,post方法甚至不会工作一次。但是我在faye服务器上接收已发布的数据。所以当我使用回调方法时,有没有办法在faye.ru文件中调用rails post方法。
这是我的faye.ru
require 'faye'
require File.expand_path('../config/initializers/faye_token.rb', __FILE__)
Faye::WebSocket.load_adapter('thin')
Faye.logger = Logger.new(STDOUT)
class ServerAuth
def incoming(message, callback)
if message['channel'] !~ %r{^/meta/}
if message['ext']['auth_token'] != ENV['FAYE_TOKEN']
message['error'] = 'Invalid authentication token'
end
end
callback.call(message)
end
# IMPORTANT: clear out the auth token so it is not leaked to the client
def outgoing(message, callback)
if message['ext'] && message['ext']['auth_token']
message['ext'] = {}
end
callback.call(message)
end
end
$bayeux = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)
$bayeux.add_extension(ServerAuth.new)
run $bayeux
和我的faye.js
$(function() {
var faye = new Faye.Client('http://localhost:9292/faye');
faye.subscribe("/functional", function(data) {
ajax_call(data);
});
});
答案 0 :(得分:0)
您可以在faye.ru中使用uri和http
require "uri"
require "net/http"
params = {"param1":"param2"}
x = Net::HTTP.post_form(URI.parse('http://your-post url'), params)
puts x.body