Faye 不能正常工作
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
(translated from Russian)。所以当我发送消息时,整个页面仍然重新加载,我需要重新加载一个页面才能看到其他人发送的消息。
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 中的版本可以访问 localhost
问题出在Csrf保护,所以话题继续here
我正在写一个简单的实时聊天,所以我决定使用网络套接字。我用了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
(translated from Russian)。所以当我发送消息时,整个页面仍然重新加载,我需要重新加载一个页面才能看到其他人发送的消息。
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 中的版本可以访问 localhost
问题出在Csrf保护,所以话题继续here