基本完成app和user的ws,加载完成时连接更多ws

This commit is contained in:
MrZ626
2021-03-27 23:50:37 +08:00
parent 8ca05bbb43
commit 8891367791
2 changed files with 121 additions and 122 deletions

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,8 @@ do
]] ]]
function TICK_WS_app() function TICK_WS_app()
local initial=true
local retryTime=5
while true do while true do
YIELD() YIELD()
local status=WS.status("app") local status=WS.status("app")
@@ -1224,25 +1123,79 @@ do
if op=="ping"then if op=="ping"then
WS.send("app",message,"pong") WS.send("app",message,"pong")
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>=message.lowest then if VERSION_CODE>=res.lowest then
ALLOW_ONLINE=true ALLOW_ONLINE=true
end end
if VERSION_CODE<message.newestCode then if VERSION_CODE<res.newestCode then
LOG.print(text.oldVersion:gsub("$1",message.newestName),180,COLOR.sky) LOG.print(text.oldVersion:gsub("$1",res.newestName),180,COLOR.sky)
end end
LOG.print(message.notice,300,COLOR.sky) 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
if retryTime==0 then return end
if initial then
for _=1,120 do YIELD()end
initial=false
end
WS.connect("app","/app") WS.connect("app","/app")
end end
end end
end end
function TICK_WS_user()
while true do
YIELD()
local status=WS.status("chat")
if status=="running"then
local message,op=WS.read("chat")
if message then
if op=="ping"then
WS.send("chat",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.messages=="Connected"then
LOGIN=true
USER.authToken=res.authToken
USER.id=res.id
FILE.save(USER,"conf/user","q")
LOG.print(text.loginSuccessed)
WS.send("user",JSON.encode{
action=0,
})
WS.send("user",JSON.encode{
action=1,
data={
id=USER.id,
},
})
elseif res.action==0 then
USER.accessToken=res.accessToken
FILE.save(USER,"conf/user")
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
function TICK_WS_chat() function TICK_WS_chat()
while true do while true do
YIELD() YIELD()
@@ -1253,19 +1206,63 @@ do
if op=="ping"then if op=="ping"then
WS.send("chat",message,"pong") WS.send("chat",message,"pong")
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

@@ -179,13 +179,15 @@ local loadingThread=coroutine.create(function()
--Connect to server --Connect to server
TASK.new(TICK_WS_app) TASK.new(TICK_WS_app)
TASK.new(TICK_WS_user)
--[[TODO TASK.new(TICK_WS_chat)
WS.send("user",JSON.encode{ 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()