5

我正在使用 private_pub 来实现一对一的类似聊天的应用程序。

这是我的故事:作为用户,我希望在我的伙伴离开聊天时收到一条消息——关闭窗口等。

在这里查看Faye Monitoring 文档是我尝试绑定unsubscribe


# Run with: rackup private_pub.ru -s thin -E production
require "bundler/setup"
require "yaml"
require "faye"
require "private_pub"
require "active_support/core_ext"

Faye::WebSocket.load_adapter('thin')

PrivatePub.load_config(File.expand_path("../config/private_pub.yml", __FILE__),     ENV["RAILS_ENV"] || "development")

wts_pubsub = PrivatePub.faye_app

wts_pubsub.bind(:subscribe) do |client_id, channel|
puts "[#{Time.now}] Client #{client_id} joined  #{channel}"
end

wts_pubsub.bind(:unsubscribe) do |client_id, channel|
  puts "[#{Time.now}] Client #{client_id} disconnected from #{channel}"
  PrivatePub.publish_to channel, { marius_says: 'quitter' }
end

run wts_pubsub

但我不断超时:[ERROR] [Faye::RackAdapter] Timeout::Error

窥探PrivatePub#publish_to,当我从 Rails 或 private_pub 应用程序发布时,数据包含我所期望的,但 private_pub 应用程序一直挂起。

我怎样才能从 private_pub 发布工作?

4

2 回答 2

0

您的第二个绑定应该是disconnectevent 而不是unsubscribe.

disconnect另外,请记住在浏览器窗口关闭时在客户端代码中 触发 Faye/PrivatePub事件。

注意:您可能需要对与 Faye 服务器的所有打开会话执行此操作,或者仅基于聊天应用程序的设计逐个通道执行此操作

在普通的 JS 中,这可能类似于:

window.onbeforeunload = functionThatTriggersFayeDisconnectEvent;

抱歉没有使用正确的标记,从手机发帖。

于 2013-04-30T23:17:59.140 回答
0

经过数小时的研究和多次尝试,这是我找到的解决方案:

替换PrivatePub.publish_to channel, { marius_says: 'quitter' }为:

system "curl http://localhost:9292/faye -d 'message={\"channel\":\"#{channel}\", \"data\":{\"channel\":\"#{channel}\",\"data\":{\"message\":{\"content\":\"#{client_id} disconnected from this channel.\"}}}, \"ext\":{\"private_pub_token\":\"ADD_APPROPRIATE_SECRET_HERE\"}}' &"

这将触发一个异步请求(curl + &),从而绕过该问题。不是最好的解决方法,但它有效。

于 2013-08-02T14:13:16.490 回答