本文介绍了Faye不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个简单的实时聊天,所以我决定使用web-sockets。我使用 gem'faye-rails'。 Web控制台显示它工作 POST http://khersonchat.herokuapp.com/faye [HTTP / 1.1 200 OK 170ms] 但是当我发送消息时有一个错误: code>加载页面连接时ws://khersonchat.herokuapp.com/faye已损坏(从俄语翻译成 )。因此,当我发送邮件时,整个网页仍然重新载入,我需要重新载入网页以查看其他人的邮件发送。



messages_controller.rb

  def create 
respond_to do | format |
if current_user
@message = current_user.messages.build(message_params)
如果@ message.save
flash [:success] ='消息已发送'
else
flash [:error] ='糟糕,错误:('
end
format.html {redirect_to root_path}
format.js
else
格式.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

咖啡

  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?日志无法取消订阅

client.subscribe'/ messages',(有效内容) - >
$('#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 [type ='submit'])val('Send')。prop('disabled',false)
});

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

- 最新源代码



重要! / strong>,该聊天使用社交网络 Vkontakte(vk.com)通过 omniauth 验证,因此存储在git中的版本可以访问localhost

解决方案

问题是 Csrf保护,所以主题继续


I'm writing a simple real-time chat, so I decided to use web-sockets. I used gem 'faye-rails'. Web console shows that it works POST http://khersonchat.herokuapp.com/faye [HTTP/1.1 200 OK 170ms] but when I send a message there's an error: While loading the page connection ws://khersonchat.herokuapp.com/faye was broken(translated from Russian). So when I send a message, the whole page still reloads, and I need to reload a page to see other people's messages sent.

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 - up-to-date source code

important! the chat authenticates via omniauth, using a social network Vkontakte(vk.com), so the version stored in git has the access to localhost

解决方案

The problem was with Csrf Protection, so the topic continues here

这篇关于Faye不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:23
查看更多