簡體   English   中英

王菲工作不正常

[英]Faye doesn't work properly

我正在編寫一個簡單的實時聊天,所以我決定使用網絡套接字。 我使用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從俄語翻譯 )。 因此,當我發送消息時,整個頁面仍會重新加載,因此我需要重新加載頁面才能查看其他人發送的消息。

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-最新源代碼

重要! 聊天使用社交網絡Vkontakte(vk.com)通過omniauth進行身份驗證,因此git中存儲的版本可以訪問localhost

問題出在Csrf Protection上 ,因此主題在這里繼續

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM