所以我在我的应用程序中使用了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);
});
});发布于 2017-02-24 18:55:35
您可以在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.bodyhttps://stackoverflow.com/questions/42435964
复制相似问题