Ruby on rails Rails,ActionController::Live,Puma:ThreadError
我想向客户端发送流式通知。为此,我使用RedisRuby on rails Rails,ActionController::Live,Puma:ThreadError,ruby-on-rails,multithreading,redis,live-streaming,puma,Ruby On Rails,Multithreading,Redis,Live Streaming,Puma,我想向客户端发送流式通知。为此,我使用Redispup/sub和ActionController::Live。以下是我的StreamingController的外观: class StreamingController < ActionController::Base include ActionController::Live def stream response.headers['Content-Type'] = 'text/event-stream' $
pup/sub
和ActionController::Live
。以下是我的StreamingController
的外观:
class StreamingController < ActionController::Base
include ActionController::Live
def stream
response.headers['Content-Type'] = 'text/event-stream'
$redis.psubscribe("user-#{params[:user_id]}:*") do |on|
on.pmessage do |subscription, event, data|
response.stream.write "data: #{data}\n\n"
end
end
rescue IOError
logger.info "Stream closed"
ensure
response.stream.close
end
end
现在,如果我将某个内容推送到redis,客户端将收到推送通知。所以这很好用。但当我点击一个链接时,什么都没有发生。在用Ctrl+C取消rails服务器(我使用puma)后,我出现以下错误:
ThreadError:尝试解锁被另一个线程锁定的互斥锁
将config.middleware.delete Rack::Lock
添加到development.rb之后,这个问题就可以解决了,但是在推送到客户端之后,我看不到任何控制台输出config.cache\u classes=true
和config.eager\u load=true
没有选项,因为我不想每次在开发中都重新启动服务器
还有其他解决方案吗?如果您不想重新启动服务器以获取更改,那么我认为您需要运行多个进程
var source = new EventSource("/stream?user_id=" + user_id);
source.addEventListener("message", function(e) {
data = jQuery.parseJSON(e.data);
switch(data.type) {
case "unread_receipts":
updateUnreadReceipts(data);
break;
}
}, false);