Merge branch 'ws_test' into main

This commit is contained in:
MrZ626
2021-03-28 02:18:58 +08:00
7 changed files with 174 additions and 177 deletions

View File

@@ -511,6 +511,7 @@ function love.run()
if SCN.swapping then SCN.swapUpdate()end--Scene swapping animation if SCN.swapping then SCN.swapUpdate()end--Scene swapping animation
WIDGET.update()--Widgets animation WIDGET.update()--Widgets animation
LOG.update() LOG.update()
WS.update()
--DRAW --DRAW
if not MINI()then if not MINI()then
@@ -556,27 +557,6 @@ function love.run()
_.draw(_.time) _.draw(_.time)
end end
--Draw network working status
gc_push("transform")
gc.translate(SCR.w,0)
gc.scale(SCR.k)
for i=1,5 do
local status=WS.status(WSnames[i])
gc_setColor(WScolor[i])
gc_rectangle("fill",0,20*i,-20,-20)
if status=="dead"then
gc_setColor(.8,.8,.8)
gc_draw(TEXTURE.ws_dead,-20,20*i-20)
elseif status=="connecting"then
gc_setColor(.8,.8,.8,.5+.3*sin(t*6.26))
gc_draw(TEXTURE.ws_connecting,-20,20*i-20)
elseif status=="running"then
gc_setColor(.8,.8,.8)
gc_draw(TEXTURE.ws_running,-20,20*i-20)
end
end
gc_pop()
--Draw FPS --Draw FPS
gc_setColor(1,1,1) gc_setColor(1,1,1)
setFont(15) setFont(15)
@@ -585,17 +565,43 @@ function love.run()
--Debug info. --Debug info.
if devMode then if devMode then
--Left-down infos
gc_setColor(devColor[devMode]) gc_setColor(devColor[devMode])
gc_print("MEM "..gcinfo(),SCR.safeX+5,_-40) gc_print("MEM "..gcinfo(),SCR.safeX+5,_-40)
gc_print("Lines "..FREEROW.getCount(),SCR.safeX+5,_-60) gc_print("Lines "..FREEROW.getCount(),SCR.safeX+5,_-60)
gc_print("Cursor "..int(mx+.5).." "..int(my+.5),SCR.safeX+5,_-80) gc_print("Cursor "..int(mx+.5).." "..int(my+.5),SCR.safeX+5,_-80)
gc_print("Voices "..VOC.getQueueCount(),SCR.safeX+5,_-100) gc_print("Voices "..VOC.getQueueCount(),SCR.safeX+5,_-100)
gc_print("Tasks "..TASK.getCount(),SCR.safeX+5,_-120) gc_print("Tasks "..TASK.getCount(),SCR.safeX+5,_-120)
--Update & draw frame time
ins(frameTimeList,1,dt)rem(frameTimeList,126) ins(frameTimeList,1,dt)rem(frameTimeList,126)
gc_setColor(1,1,1,.3) gc_setColor(1,1,1,.3)
for i=1,#frameTimeList do for i=1,#frameTimeList do
gc_rectangle("fill",150+2*i,_-20,2,-frameTimeList[i]*4000) gc_rectangle("fill",150+2*i,_-20,2,-frameTimeList[i]*4000)
end end
--Websocket status
gc_push("transform")
gc.translate(SCR.w,0)
gc.scale(SCR.k)
for i=1,5 do
local status=WS.status(WSnames[i])
gc_setColor(WScolor[i])
gc_rectangle("fill",0,20*i,-20,-20)
if status=="dead"then
gc_setColor(.8,.8,.8)
gc_draw(TEXTURE.ws_dead,-20,20*i-20)
elseif status=="connecting"then
gc_setColor(.8,.8,.8,.5+.3*sin(t*6.26))
gc_draw(TEXTURE.ws_connecting,-20,20*i-20)
elseif status=="running"then
gc_setColor(.8,.8,.8)
gc_draw(TEXTURE.ws_running,-20,20*i-20)
end
end
gc_pop()
--Slow devmode
if devMode==3 then WAIT(.1) if devMode==3 then WAIT(.1)
elseif devMode==4 then WAIT(.5) elseif devMode==4 then WAIT(.5)
end end

View File

@@ -72,18 +72,19 @@ do--Connect
--First line of HTTP --First line of HTTP
local l=SOCK:receive("*l") local l=SOCK:receive("*l")
local code=l:find(" "); code=l:sub(code+1,code+3) local code,ctLen
if l then
if code=="101"then code=l:find(" "); code=l:sub(code+1,code+3)
readCHN:push("success")
else
local ctLen
repeat repeat
l=SOCK:receive("*l") l=SOCK:receive("*l")
if not ctLen and l:find"Length"then if not ctLen and l:find"Length"then
ctLen=tonumber(l:match"%d+") ctLen=tonumber(l:match"%d+")
end end
until l=="" until l==""
end
if code=="101"then
readCHN:push("success")
else
local reason=JSON.decode(SOCK:receive(ctLen)) local reason=JSON.decode(SOCK:receive(ctLen))
readCHN:push(code..":"..(reason and reason.message or"Server Error")) readCHN:push(code..":"..(reason and reason.message or"Server Error"))
end end
@@ -222,7 +223,7 @@ function WS.update()
ws.lastPongTime=time ws.lastPongTime=time
else else
ws.status="dead" ws.status="dead"
LOG.print(text.wsFailed,"warn") LOG.print(text.wsFailed.." "..mes,"warn")
end end
end end
elseif time-ws.lastPingTime>ws.pingInterval then elseif time-ws.lastPingTime>ws.pingInterval then

View File

@@ -26,7 +26,7 @@ DAILYLAUNCH=false
LOGIN=false LOGIN=false
EDITING="" EDITING=""
WSCONN=false WSCONN=false
LATEST_VERSION=false ALLOW_ONLINE=false
ERRDATA={} ERRDATA={}
--System setting --System setting

View File

@@ -1066,19 +1066,6 @@ end
--Network funcs --Network funcs
do do
--[[ --[[
launch:
local res=JSON.decode(res.body)
if res.message=="OK"then
LOG.print(res.notice,360,COLOR.sky)
if VERSION_CODE>=res.version_code then
LATEST_VERSION=true
else
LOG.print(string.gsub(text.oldVersion,"$1",res.version_name),"warn")
end
else
LOG.print(text.httpCode..res.code..": "..res.message,"warn")
end
register: register:
if response.message=="OK"then if response.message=="OK"then
LOGIN=true LOGIN=true
@@ -1092,95 +1079,6 @@ do
LOG.print(text.httpCode..response.code..": "..res.message,"warn") LOG.print(text.httpCode..response.code..": "..res.message,"warn")
end end
autoLogin:
if res.message=="OK"then
LOGIN=true
LOG.print(text.loginSuccessed)
httpRequest(
TICK_httpREQ_getUserInfo,
PATH.http..PATH.user,
"GET",
{["Content-Type"]="application/json"},
JSON.encode{
email=USER.email,
authToken=USER.authToken,
}
)
else
LOGIN=false
LOG.print(text.loginFailed..": "..text.httpCode..res.code.."-"..res.message,"warn")
end
return
newLogin:
if res.message=="OK"then
LOGIN=true
USER.email=res.email
USER.authToken=res.authToken
USER.id=res.id
FILE.save(USER,"conf/user","q")
LOG.print(text.loginSuccessed)
--TODO:getUserInfo
JSON.encode{
email=USER.email,
authToken=USER.authToken,
}
--TODO:getAccessToken
JSON.encode{
email=USER.email,
authToken=USER.authToken,
}
else
LOG.print(text.httpCode..res.code..": "..res.message,"warn")
end
manualAutoLogin:
if res.message=="OK"then
LOG.print(text.accessSuccessed)
SCN.go("net_menu")
elseif res.code==403 or res.code==401 then
httpRequest(
TICK_httpREQ_getAccessToken,
PATH.http..PATH.access,
"POST",
{["Content-Type"]="application/json"},
JSON.encode{
email=USER.email,
authToken=USER.authToken,
}
)
else
local err=JSON.decode(res.body)
if err then
LOG.print(text.httpCode..res.code..": "..err.message,"warn")
end
end
getAccessToken:
if res.message=="OK"then
LOG.print(text.accessSuccessed)
USER.accessToken=res.accessToken
FILE.save(USER,"conf/user")
SCN.swapTo("net_menu")
else
LOGIN=false
USER.accessToken=false
USER.authToken=false
LOG.print(text.loginFailed..": "..text.httpCode..response.code.."-"..res.message,"warn")
end
getUserInfo:
if res.message=="OK"then
USER.name=res.username
USER.motto=res.motto
USER.avatar=res.avatar
FILE.save(USER,"conf/user")
else
LOG.print("Get user info failed: "..text.httpCode..response.code.."-"..res.message,"warn")
end
goChatRoom: goChatRoom:
if res.message=="OK"then if res.message=="OK"then
SCN.go("net_chat") SCN.go("net_chat")
@@ -1204,7 +1102,6 @@ do
LOG.print(text.httpCode..res.code..": "..res.message,"warn") LOG.print(text.httpCode..res.code..": "..res.message,"warn")
end end
enterRoom: enterRoom:
if res.message=="OK"then if res.message=="OK"then
loadGame("netBattle",true,true) loadGame("netBattle",true,true)
@@ -1215,6 +1112,7 @@ do
]] ]]
function TICK_WS_app() function TICK_WS_app()
local retryTime=5
while true do while true do
YIELD() YIELD()
local status=WS.status("app") local status=WS.status("app")
@@ -1223,20 +1121,76 @@ do
if message then if message then
if op=="ping"then if op=="ping"then
WS.send("app",message,"pong") WS.send("app",message,"pong")
--TODO: ping animation
--TODO: what to do with res?
elseif op=="close"then elseif op=="close"then
LOG.print(text.wsClose..message,"warn") message=JSON.decode(message)
if message then
LOG.print(text.wsClose..message.message,"warn")
end
return return
else else
message=JSON.decode(message) local res=JSON.decode(message)
if VERSION_CODE>=res.lowest then
ALLOW_ONLINE=true
end
if VERSION_CODE<res.newestCode then
LOG.print(text.oldVersion:gsub("$1",res.newestName),180,COLOR.sky)
end
LOG.print(res.notice,300,COLOR.sky)
end end
end end
elseif status=="dead"then elseif status=="dead"then
for _=1,60 do YIELD()end retryTime=retryTime-1
WS.connect("app","/app",JSON.encode{ if retryTime==0 then return end
version=VERSION_CODE, for _=1,120 do YIELD()end
}) WS.connect("app","/app")
end
end
end
function TICK_WS_user()
while true do
YIELD()
local status=WS.status("user")
if status=="running"then
local message,op=WS.read("user")
if message then
if op=="ping"then
WS.send("user",message,"pong")
elseif op=="close"then
message=JSON.decode(message)
if message then
LOG.print(text.wsClose..message.message,"warn")
end
return
else
local res=JSON.decode(message)
if res.message=="Connected"then
LOGIN=true
if res.id then
USER.id=res.id
USER.authToken=res.authToken
SCN.go("net_menu")
end
FILE.save(USER,"conf/user","q")
LOG.print(text.loginSuccessed)
--Get self infos
WS.send("user",JSON.encode{
action=1,
data={
id=USER.id,
},
})
elseif res.action==0 then
USER.accessToken=res.accessToken
LOG.print(text.accessSuccessed)
elseif res.action==1 then
USER.name=res.username
USER.motto=res.motto
USER.avatar=res.avatar
FILE.save(USER,"conf/user")
end
end
end
end end
end end
end end
@@ -1249,22 +1203,64 @@ do
if message then if message then
if op=="ping"then if op=="ping"then
WS.send("chat",message,"pong") WS.send("chat",message,"pong")
--TODO: ping animation
--TODO: what to do with res?
elseif op=="close"then elseif op=="close"then
LOG.print(text.wsClose..message,"warn") message=JSON.decode(message)
if message then
LOG.print(text.wsClose..message.message,"warn")
end
return return
else else
message=JSON.decode(message) local res=JSON.decode(message)
--TODO
end
end
end
end
end
function TICK_WS_play()
while true do
YIELD()
local status=WS.status("play")
if status=="running"then
local message,op=WS.read("play")
if message then
if op=="ping"then
WS.send("play",message,"pong")
elseif op=="close"then
message=JSON.decode(message)
if message then
LOG.print(text.wsClose..message.message,"warn")
end
return
else
local res=JSON.decode(message)
--TODO
end
end
end
end
end
function TICK_WS_stream()
while true do
YIELD()
local status=WS.status("stream")
if status=="running"then
local message,op=WS.read("stream")
if message then
if op=="ping"then
WS.send("stream",message,"pong")
elseif op=="close"then
message=JSON.decode(message)
if message then
LOG.print(text.wsClose..message.message,"warn")
end
return
else
local res=JSON.decode(message)
--TODO
end end
end end
elseif status=="dead"then
for _=1,60 do YIELD()end
WS.connect("chat","/chat",JSON.encode{
version=VERSION_CODE,
})
end end
end end
end end
--TODO: more WSs
end end

View File

@@ -176,12 +176,18 @@ local loadingThread=coroutine.create(function()
end end
STAT.run=STAT.run+1 STAT.run=STAT.run+1
LOADED=true LOADED=true
--[[TODO
WS.send("user",JSON.encode{ --Connect to server
TASK.new(TICK_WS_app)
TASK.new(TICK_WS_user)
WS.connect("app","/app")
if USER.authToken then
WS.connect("user","/user",JSON.encode{
id=USER.id, id=USER.id,
authToken=USER.authToken, authToken=USER.authToken,
}) })
]] end
while true do while true do
if math.random()<.126 then if math.random()<.126 then
upFloor() upFloor()

View File

@@ -6,20 +6,19 @@ local function login()
elseif #password==0 then elseif #password==0 then
LOG.print(text.noPassword)return LOG.print(text.noPassword)return
end end
--[[TODO USER.email=email
WS.send("user",JSON.encode{ WS.connect("user","/user",JSON.encode{
email=email, email=email,
password=password, password=password,
}) })
]]
end end
local scene={} local scene={}
scene.widgetList={ scene.widgetList={
WIDGET.newText{name="title", x=80, y=50,font=70,align="L"}, WIDGET.newText{name="title", x=80, y=50,font=70,align="L"},
-- WIDGET.newButton{name="register", x=1140, y=100,w=170,h=80,color="green",code=function()SCN.swapTo("register","swipeR")end}, -- WIDGET.newButton{name="register",x=1140, y=100,w=170,h=80,color="green",code=function()SCN.swapTo("register","swipeR")end},
WIDGET.newInputBox{name="email", x=380, y=200,w=500,h=60,regex="[0-9A-Za-z@._-]"}, WIDGET.newInputBox{name="email", x=380, y=200,w=500,h=60,regex="[0-9A-Za-z@._-]"},
WIDGET.newInputBox{name="password", x=380, y=300,w=626,h=60,secret=true,regex="[ -~]"}, WIDGET.newInputBox{name="password", x=380, y=300,w=626,h=60,secret=true,regex="[ -~]"},
WIDGET.newKey{name="login", x=1140, y=540,w=170,h=80,font=40,code=login}, WIDGET.newKey{name="login", x=1140, y=540,w=170,h=80,font=40,code=login},
WIDGET.newButton{name="back", x=1140, y=640,w=170,h=80,font=40,code=backScene}, WIDGET.newButton{name="back", x=1140, y=640,w=170,h=80,font=40,code=backScene},

View File

@@ -77,25 +77,14 @@ function scene.keyDown(key)
end end
elseif key=="a"then elseif key=="a"then
if testButton(3)then if testButton(3)then
if not LATEST_VERSION then if not ALLOW_ONLINE then
TEXT.show(text.notFinished,640,450,60,"flicker") TEXT.show(text.notFinished,640,450,60,"flicker")
SFX.play("finesseError") SFX.play("finesseError")
elseif LOGIN then elseif LOGIN then
--[[TODO SCN.go("net_menu")
if USER.accessToken then WS.send("user",JSON.encode{
WS.send("app",JSON.encode{ action=0,
opration="access", })
email=USER.email,
accessToken=USER.accessToken,
})
else
WS.send("app",JSON.encode{
opration="access",
email=USER.email,
authToken=USER.authToken,
})
end
]]
else else
SCN.go("login") SCN.go("login")
end end