2016-04-02 41 views
0

我正在写一个简单的实时聊天,所以我决定使用网络套接字。我用gem 'faye-rails'。 Web控制台显示它可以工作POST http://khersonchat.herokuapp.com/faye [HTTP/1.1 200 OK 170ms]但是当我发送消息时出现错误:While loading the page connection ws://khersonchat.herokuapp.com/faye was broken翻译自俄文)。所以当我发送消息时,整个页面仍然会重新加载,我需要重新加载页面才能看到其他人发送的消息。Faye无法正常工作

messages_controller.rb

def create 
    respond_to do |format| 
     if current_user 
      @message = current_user.messages.build(message_params) 
      if @message.save 
       flash[:success] = 'Message sent' 
      else 
       flash[:error] = 'Oops, an error :(' 
      end 
      format.html {redirect_to root_path} 
      format.js 
     else 
      format.html {redirect_to root_path} 
      format.js {render nothing: true} 
     end 
    end 
end 

application.js

//= require jquery 
//= require jquery_ujs 
//= require faye 
//= require messages 
//= require_self 
//= require turbolinks 

messages.coffee

window.client = new Faye.Client('/faye') 

jQuery -> 
    $('#new_message').submit -> 
     $(this).find("input[type='submit']").val('Sending...').prop('disabled', true) 
    try 
     client.unsubscribe('/messages') 
    catch 
     console?.log "Can't unsubscribe" 

    client.subscribe '/messages', (payload) -> 
     $('#messages').find('.media-list').append(payload.message) if payload.message 

create.js.erb

publisher = client.publish('/messages', { 
    message: '<%= j render @message %>' 
}); 

publisher.callback(function() { 
    $("#message_body").val(''); 
    $("#new_message").find("input[type='submit']").val('Send').prop('disabled', false) 
}); 

publisher.errback(function() { 
    alert('Oops, an error'); 
}); 

https://github.com/AlexNikolaev94/chatclone.git - 最多最新的源代码

重要!聊天通过omniauth验证,使用社交网络的Vkontakte (vk.com),所以存储在git的版本有访问到本地主机

+0

Faye似乎没有让消息发布,我完全不知道发生了什么事情:(双三重检查了一切,但它根本不起作用 – AlexNikolaev94

回答