Nginx Redis长轮询发布/次频繁消息阻止
我正试图绕过Redis发布/订阅API,设置一个长轮询服务器 此lua脚本订阅“测试”通道并返回收到的新消息: nginx.conf:Nginx Redis长轮询发布/次频繁消息阻止,nginx,lua,redis,Nginx,Lua,Redis,我正试图绕过Redis发布/订阅API,设置一个长轮询服务器 此lua脚本订阅“测试”通道并返回收到的新消息: nginx.conf: location /poll { lua_need_request_body on; default_type 'text/plain'; content_by_lua_file '/usr/local/nginx/html/poll.lua'; } poll.lua: local redis = require "redis"; local re
location /poll {
lua_need_request_body on;
default_type 'text/plain';
content_by_lua_file '/usr/local/nginx/html/poll.lua';
}
poll.lua:
local redis = require "redis";
local red = redis:new();
local cjson = require "cjson";
red:set_timeout(30000) -- 30 sec
local resCon, err = red:connect("127.0.0.1", 6379)
if not resCon then
ngx.print("error")
return
end
local resSub, err = red:subscribe('r:' .. ngx.var["arg_r"]:gsub('%W',''))
if not resSub then
ngx.print("error")
return
end
if resSub == ngx.null then
ngx.print("error")
return
end
local resMsg, err = red:read_reply()
if not resMsg then
ngx.say("0")
return
end
ngx.say(cjson.encode(resMsg))
client.js:
var tmpR = 'test';
function poll() {
$.get('/poll', {'r':tmpR}, function(data){
if (data !== "error") {
console.log(data);
window.setTimeout(function(){
poll();
},1000);
} else {
console.log('poll fail');
}
})
}
现在,如果我从redis cli发送publish r:test hello
,我会在客户端收到消息,服务器会用1
响应redis cli。但是,如果我快速发送两条消息,则第二条消息不会广播,服务器响应0
我的频道每秒只能接收一条消息吗?或者,这是对用户可以向频道广播的消息频率的限制吗
假设一次可以连接多个用户,这是在nginx上访问此轮询服务器的正确方法吗?在计时器上使用GET请求会更有效吗?给定两条连续消息,只有一条会让订户监听结果。发送第二条消息时,没有订户正在侦听。唯一的订户正在处理以前的结果并将其返回给用户
Redis未维护消息队列或类似队列,以确保先前侦听的客户端在重新连接时将收到丢失的消息。请详细说明这一点?我刚刚用两个订阅者(同一台计算机上的Safari/Chrome)进行了测试。当我从控制台使用
publish r:test hello
时,服务器会执行其中一项操作(它们之间似乎是交替的)。要么它响应2
,我在两个位置都收到消息,要么它说1
,它只发送给一个客户端。我不太明白它为什么这么做。理想情况下,我会订阅x个客户端,然后它们都会收到我从控制台发送的消息。(这是因为两个客户端都位于同一IP上,并计为1个订户,那么当第一个客户端收到消息时,它不会第二次发送到同一IP?尽管如此,为什么有时会起作用?对于频繁发送到单个客户端的消息,在第一条消息传入后重新连接到第二条消息之间是否会丢弃消息?重新连接应该不会花那么长时间。嗯,我不太明白发生了什么事情,也不知道这是怎么回事。)“对于频繁发送到单个客户端的消息,在第一条消息传入后重新连接到第二条消息之间是否会断开消息?”是的,我想是这样的。啊,当我发送一条消息并且只在一个地点收到它时,它似乎也源于频率。我想我只是错过了连接之间发送的消息。好的,现在这是有意义的。谢谢!