升级task模块和使用模块的网络相关函数,ws连接任务不再与未完成的http请求冲突

This commit is contained in:
MrZ626
2021-02-09 17:59:27 +08:00
parent cab826ba34
commit 5073fe5363
3 changed files with 22 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ local tasks={}
local TASK={ local TASK={
netTaskCount=0, netTaskCount=0,
wsConnecting=false,
} }
function TASK.getCount() function TASK.getCount()
return #tasks return #tasks
@@ -17,6 +18,9 @@ function TASK.update()
if T.net then if T.net then
TASK.netTaskCount=TASK.netTaskCount-1 TASK.netTaskCount=TASK.netTaskCount-1
end end
if T.ws then
TASK.wsConnecting=false
end
rem(tasks,i) rem(tasks,i)
end end
end end
@@ -44,6 +48,20 @@ function TASK.newNet(code,...)
} }
end end
end end
function TASK.newWS(code,...)
TASK.wsConnecting=true
local thread=ct.create(code)
ct.resume(thread,...)
if ct.status(thread)~="dead"then
tasks[#tasks+1]={
thread=thread,
code=code,
args={...},
net=true,
ws=true,
}
end
end
function TASK.removeTask_code(code) function TASK.removeTask_code(code)
for i=#tasks,1,-1 do for i=#tasks,1,-1 do
if tasks[i].code==code then if tasks[i].code==code then

View File

@@ -559,7 +559,7 @@ do--httpRequest & wsConnect
end end
function wsConnect(tick,path,header) function wsConnect(tick,path,header)
if TASK.netTaskCount>0 then if TASK.wsConnecting then
LOG.print(text.waitNetTask,"message") LOG.print(text.waitNetTask,"message")
return return
end end
@@ -569,7 +569,7 @@ do--httpRequest & wsConnect
header=header, header=header,
} }
if task then if task then
TASK.newNet(tick,task) TASK.newWS(tick,task)
else else
LOG.print("NETlib error: "..err,"warn") LOG.print("NETlib error: "..err,"warn")
end end

View File

@@ -28,7 +28,6 @@ local function task_enterRoom(task)
end end
end end
local function enterRoom(roomID) local function enterRoom(roomID)
coroutine.yield()
wsConnect( wsConnect(
task_enterRoom, task_enterRoom,
PATH.socket..PATH.onlinePlay.. PATH.socket..PATH.onlinePlay..
@@ -72,7 +71,7 @@ local function task_createRoom(task)
local res=json.decode(response.body) local res=json.decode(response.body)
if response.code==200 and res.message=="OK"then if response.code==200 and res.message=="OK"then
LOG.print(text.createRoomSuccessed) LOG.print(text.createRoomSuccessed)
TASK.newNet(enterRoom,res.room.id) enterRoom(res.room.id)
else else
LOG.print(text.httpCode..response.code..": "..res.message,"warn") LOG.print(text.httpCode..response.code..": "..res.message,"warn")
end end