与Faye和Rails的私人消息 [英] Private messages with Faye and Rails

查看:108
本文介绍了与Faye和Rails的私人消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 faye gem /episodes/260-messaging-with-faye"rel =" nofollow>铁轨,允许应用推送消息.问题是它将消息推送到所有打开的聊天客户端.我需要他们是私人的.可以通过faye获取私人消息,但它基于url.例如,所有消息将发送到site.com/foo.但是,在我的模型中,聊天没有特定的网址.因为聊天只是该用户发送给您的消息的集合.

I'm using the faye gem menitioned in the railscast allowing apps to push messages. Problem is it pushes messages to all chat clients that are open. I need them to be private. It's possible to get private messaging with faye but it's url based. For example all messages will be sent to site.com/foo. But, in my model the chat doesn't have a specific url. Because chat is just a collection of messages sent to you by that user.

因此,如果您以亚当site.com/messages/eve身份登录,将允许您与eve交谈,但对于eve而言,则相反. site.com/messages/adam.因此,特定于URL的私人聊天似乎是不可能的.

So if you're logged in as adam site.com/messages/eve would allow you to talk to eve but for eve it's the reverse. site.com/messages/adam. So URL specific private chat is seemingly out of the question.

有指针吗?

messages_controller.rb (触发ajax)

def create
  @message = Message.new(message_params)
  @message.save
end 

create.js.erb (调用广播方法)

<% broadcast params[:message][:username] do %>
  $(".text-wrap").append("<%= current_user.name %> <%= @message.body %></div>");
<% end %>

广播方法(发布到faye服务器)

broadcast method ( posting to the faye server )

def broadcast(user, &block)
  channel = "/messages/#{user}"
  message = {:channel => channel, :data => capture(&block)}
  uri = URI.parse("http://localhost:9292/faye")
  Net::HTTP.post_form(uri, :message => message.to_json)
end

application.js (faye服务器订阅/messages/*)

application.js ( faye server subscription to /messages/* )

$(function(){
    var faye = new Faye.Client('http://localhost:9292/faye');
    var subscription = faye.subscribe('/messages/*', function(message) {
        eval(message);
    });
});

推荐答案

我解决了!问题是我在application.js中具有订阅功能,这意味着它将在每个页面上运行订阅javascript.相反,我所做的是在我预订的/messages/eve/adam聊天页面视图的底部.这是我的方法:

I solved it! The problem was that I had the subscribe function in application.js meaning it would run the subscribe javascript on each page. Instead what I did is at the bottom of the chat page view i subscribed to /messages/eve/adam. This is how I did that:

<script>  
  $(function(){
    var faye = new Faye.Client('http://localhost:9292/faye');
    var subscription = faye.subscribe('/messages/#{current_user.username}/#{@user.username}', function(message) {
      eval(message);
    });
  });
</script>

然后使用广播方法确保将信息仅发送到正确的地方.

Then in the broadcast method I made sure to send the information to only the correct place.

def broadcast(user, &block)
  channel = "/messages/#{current_user.username}/#{user}"
  message = {:channel => channel, :data => capture(&block)}
  uri = URI.parse("http://localhost:9292/faye")
  Net::HTTP.post_form(uri, :message => message.to_json)
end 

这篇关于与Faye和Rails的私人消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆