2013-04-29 51 views
5

我正在使用private_pub實現一對一的類似聊天的應用程序。Ruby on Rails - Faye框架 - private_pub

這裏是我的故事:作爲一個用戶,我想,當我的搭檔離開聊天接收消息 - 關閉窗口等

通過Faye Monitoring docs看這是我在上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

當我從Rails或private_pub應用程序發佈時,數據保留了我期望的兩種數據,但private_pub應用程序仍然處於懸掛狀態。

我怎樣才能從private_pub發佈工作?

+0

此線程有點舊;你有沒有想過問題是什麼?我一直在這個確切的問題上花費數小時,但沒有取得太大的成功 – Etienne 2013-07-30 17:19:31

回答

0

您的第二個綁定應該是disconnect事件而不是unsubscribe

此外,請記住在瀏覽器窗口關閉時,在您的客戶端代碼中觸發Faye/PrivatePub disconnect事件。

注:您可能需要爲與王菲的服務器或只是基於聊天應用程序的設計

在純JS這可能是這樣的通道基礎通道全部打開的會話做到這一點:

window.onbeforeunload = functionThatTriggersFayeDisconnectEvent; 

對不起,沒有使用正確的標記,從移動張貼。

+0

當關閉選項卡時會自動觸發'disconnect'和'unsubscribe'事件 - 這裏是[modified private_pub.rb](https://gist.github.com/mariusbutuc/8cdd6d71e7595a2fb814#file-private_pub-rb)和[它的輸出](https://gist.github.com/mariusbutuc/8cdd6d71e7595a2fb814#file-private_pub-out)。似乎沒有解決OP的問題。 – 2013-05-01 16:36:49

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\"}}' &" 

這將觸發異步請求(捲曲+ &),其將繞過問題。不是最好的解決方法,但它的工作原理。