ws类加入发送timer并修改读取方法,更清晰的ws状态展示

This commit is contained in:
MrZ626
2021-03-29 12:29:52 +08:00
parent ee02013fa5
commit aad6dcb269
3 changed files with 25 additions and 22 deletions

View File

@@ -444,13 +444,13 @@ function love.errorhandler(msg)
end end
end end
end end
local WSnames={"app","user","chat","play","stream"} local WSnames={"app","user","play","stream","chat"}
local WScolor={ local WScolor={
{1,0,0,.26}, {1,.5,.5,.7},
{1,.7,0,.26}, {1,.8,.3,.7},
{0,.7,1,.26}, {1,1,.4,.7},
{0,1,0,.26}, {.4,1,.7,.7},
{1,1,0,.26} {.5,.8,1,.7},
} }
local devColor={ local devColor={
COLOR.white, COLOR.white,
@@ -587,19 +587,21 @@ function love.run()
for i=1,5 do for i=1,5 do
local status=WS.status(WSnames[i]) local status=WS.status(WSnames[i])
gc_setColor(WScolor[i]) gc_setColor(WScolor[i])
gc_rectangle("fill",0,20*i,-20,-20) gc_rectangle("fill",0,20*i,-80,-20)
if status=="dead"then if status=="dead"then
gc_setColor(.8,.8,.8) gc_setColor(1,1,1)
gc_draw(TEXTURE.ws_dead,-20,20*i-20) gc_draw(TEXTURE.ws_dead,-20,20*i-20)
elseif status=="connecting"then elseif status=="connecting"then
gc_setColor(.8,.8,.8,.5+.3*sin(t*6.26)) gc_setColor(1,1,1,.5+.3*sin(t*6.26))
gc_draw(TEXTURE.ws_connecting,-20,20*i-20) gc_draw(TEXTURE.ws_connecting,-20,20*i-20)
elseif status=="running"then elseif status=="running"then
gc_setColor(.8,.8,.8) gc_setColor(1,1,1)
gc_draw(TEXTURE.ws_running,-20,20*i-20) gc_draw(TEXTURE.ws_running,-20,20*i-20)
end end
gc_setColor(1,1,1,WS.getPongTimer(WSnames[i]))gc_rectangle("fill",0,20*i,-20,-20) local t1,t2,t3=WS.getTimers(WSnames[i])
gc_setColor(1,0,0,WS.getAlertTimer(WSnames[i]))gc_rectangle("fill",-4,20*i-4,-12,-12) gc_setColor(1,1,1,t1)gc_rectangle("fill",-60,20*i,-20,-20)
gc_setColor(0,1,0,t2)gc_rectangle("fill",-40,20*i,-20,-20)
gc_setColor(1,0,0,t3)gc_rectangle("fill",-20,20*i,-20,-20)
end end
gc_pop() gc_pop()

View File

@@ -164,8 +164,9 @@ local wsList=setmetatable({},{
real=false, real=false,
status="dead", status="dead",
lastPongTime=timer(), lastPongTime=timer(),
pongTimer=0, sendTimer=0,
alertTimer=0, alertTimer=0,
pongTimer=0,
} }
l[k]=ws l[k]=ws
return ws return ws
@@ -183,8 +184,9 @@ function WS.connect(name,subPath,body)
lastPongTime=timer(), lastPongTime=timer(),
pingInterval=26, pingInterval=26,
status="connecting",--connecting, running, dead status="connecting",--connecting, running, dead
pongTimer=0, sendTimer=0,
alertTimer=0, alertTimer=0,
pongTimer=0,
} }
wsList[name]=ws wsList[name]=ws
ws.thread:start(ws.triggerCHN,ws.sendCHN,ws.readCHN) ws.thread:start(ws.triggerCHN,ws.sendCHN,ws.readCHN)
@@ -199,12 +201,9 @@ function WS.status(name)
return ws.status or"dead" return ws.status or"dead"
end end
function WS.getPongTimer(name) function WS.getTimers(name)
return wsList[name].pongTimer local ws=wsList[name]
end return ws.pongTimer,ws.sendTimer,ws.alertTimer
function WS.getAlertTimer(name)
return wsList[name].alertTimer
end end
function WS.setPingInterval(name,time) function WS.setPingInterval(name,time)
@@ -239,6 +238,7 @@ function WS.send(name,message,op)
ws.sendCHN:push(op and OPcode[op]or 2)--2=binary ws.sendCHN:push(op and OPcode[op]or 2)--2=binary
ws.sendCHN:push(message) ws.sendCHN:push(message)
ws.lastPingTime=timer() ws.lastPingTime=timer()
ws.sendTimer=1
end end
end end
@@ -289,6 +289,7 @@ function WS.update(dt)
if time-ws.lastPongTime>10+3*ws.pingInterval then if time-ws.lastPongTime>10+3*ws.pingInterval then
WS.close(name) WS.close(name)
end end
if ws.sendTimer>0 then ws.sendTimer=ws.sendTimer-dt end
if ws.pongTimer>0 then ws.pongTimer=ws.pongTimer-dt end if ws.pongTimer>0 then ws.pongTimer=ws.pongTimer-dt end
if ws.alertTimer>0 then ws.alertTimer=ws.alertTimer-dt end if ws.alertTimer>0 then ws.alertTimer=ws.alertTimer-dt end
end end

View File

@@ -144,14 +144,14 @@ end
--WS icons --WS icons
setFont(20) setFont(20)
TEXTURE.ws_dead=NSC(20,20) TEXTURE.ws_dead=NSC(20,20)
gc.setColor(1,.4,.3) gc.setColor(1,.3,.3)
gc.print("X",3,-4) gc.print("X",3,-4)
TEXTURE.ws_connecting=NSC(20,20) TEXTURE.ws_connecting=NSC(20,20)
gc.setLineWidth(3) gc.setLineWidth(3)
gc.setColor(1,1,1) gc.setColor(1,1,1)
gc.arc("line","open",11.5,10,6.26,1,5.28) gc.arc("line","open",11.5,10,6.26,1,5.28)
TEXTURE.ws_running=NSC(20,20) TEXTURE.ws_running=NSC(20,20)
gc.setColor(0,.9,0) gc.setColor(.5,1,0)
gc.print("R",3,-4) gc.print("R",3,-4)