联网推进(修改玩家退出机制,部分联网命令增加超时时间)

This commit is contained in:
MrZ626
2021-04-08 22:23:19 +08:00
parent 8a1a3b971f
commit bdf1ce640c
2 changed files with 32 additions and 25 deletions

View File

@@ -86,7 +86,7 @@ function NET.wsconn_user_token(uid,authToken)
end end
end end
function NET.wsconn_play() function NET.wsconn_play()
if _lock("wsc_play")then if _lock("wsc_play",5)then
WS.connect("play","/play",JSON.encode{ WS.connect("play","/play",JSON.encode{
uid=USER.uid, uid=USER.uid,
accessToken=NET.accessToken, accessToken=NET.accessToken,
@@ -94,7 +94,7 @@ function NET.wsconn_play()
end end
end end
function NET.wsconn_stream() function NET.wsconn_stream()
if _lock("wsc_stream")then if _lock("wsc_stream",5)then
WS.connect("stream","/stream",JSON.encode{ WS.connect("stream","/stream",JSON.encode{
uid=USER.uid, uid=USER.uid,
accessToken=NET.accessToken, accessToken=NET.accessToken,
@@ -116,7 +116,7 @@ function NET.pong(wsName,message)
WS.send(wsName,message,"pong") WS.send(wsName,message,"pong")
end end
function NET.getAccessToken() function NET.getAccessToken()
if _lock("accessToken")then if _lock("accessToken",3)then
WS.send("user",JSON.encode{action=0}) WS.send("user",JSON.encode{action=0})
end end
end end
@@ -154,7 +154,7 @@ end
--Room --Room
function NET.fetchRoom() function NET.fetchRoom()
if _lock("fetchRoom")then if _lock("fetchRoom",3)then
WS.send("play",JSON.encode{ WS.send("play",JSON.encode{
action=0, action=0,
data={ data={
@@ -166,7 +166,7 @@ function NET.fetchRoom()
end end
end end
function NET.createRoom() function NET.createRoom()
if _lock("enterRoom",5)then if _lock("enterRoom",3)then
WS.send("play",JSON.encode{ WS.send("play",JSON.encode{
action=1, action=1,
data={ data={
@@ -179,7 +179,7 @@ function NET.createRoom()
end end
end end
function NET.enterRoom(roomID,password) function NET.enterRoom(roomID,password)
if _lock("enterRoom",5)then if _lock("enterRoom",3)then
NET.rid=roomID NET.rid=roomID
WS.send("play",JSON.encode{ WS.send("play",JSON.encode{
action=2, action=2,
@@ -197,12 +197,14 @@ function NET.checkPlayDisconn()
return WS.status("play")~="running" return WS.status("play")~="running"
end end
function NET.signal_ready(ready) function NET.signal_ready(ready)
if _lock("ready")then if _lock("ready",3)then
WS.send("play",'{"action":6,"data":{"ready":'..tostring(ready)..'}}') WS.send("play",'{"action":6,"data":{"ready":'..tostring(ready)..'}}')
end end
end end
function NET.signal_quit() function NET.signal_quit()
WS.send("play",'{"action":3}') if _lock("quit",3)then
WS.send("play",'{"action":3}')
end
end end
function NET.signal_die() function NET.signal_die()
WS.send("stream",'{"action":4,"data":{"score":0,"survivalTime":0}}') WS.send("stream",'{"action":4,"data":{"score":0,"survivalTime":0}}')
@@ -363,25 +365,31 @@ function NET.updateWS_play()
SCN.socketRead("Join",res.data) SCN.socketRead("Join",res.data)
end end
elseif res.action==3 then--Player leave elseif res.action==3 then--Player leave
for i=1,#PLY_NET do if not d.uid then
if PLY_NET[i].uid==d.uid then NET.wsclose_stream()
rem(PLY_NET,i) SCN.back()
break _unlock("quit")
else
for i=1,#PLY_NET do
if PLY_NET[i].uid==d.uid then
rem(PLY_NET,i)
break
end
end end
end for i=1,#PLAYERS do
for i=1,#PLAYERS do if PLAYERS[i].userID==d.uid then
if PLAYERS[i].userID==d.uid then rem(PLAYERS,i)
rem(PLAYERS,i) break
break end
end end
end for i=1,#PLY_ALIVE do
for i=1,#PLY_ALIVE do if PLY_ALIVE[i].userID==d.uid then
if PLY_ALIVE[i].userID==d.uid then rem(PLY_ALIVE,i)
rem(PLY_ALIVE,i) break
break end
end end
SCN.socketRead("Leave",res.data)
end end
SCN.socketRead("Leave",res.data)
elseif res.action==4 then--Player talk elseif res.action==4 then--Player talk
SCN.socketRead("Talk",res.data) SCN.socketRead("Talk",res.data)
elseif res.action==5 then--Player change settings elseif res.action==5 then--Player change settings

View File

@@ -21,8 +21,6 @@ local touchMoveLastFrame=false
local scene={} local scene={}
function scene.sceneBack() function scene.sceneBack()
NET.signal_quit()
NET.wsclose_stream()
love.keyboard.setKeyRepeat(true) love.keyboard.setKeyRepeat(true)
end end
function scene.sceneInit() function scene.sceneInit()
@@ -78,6 +76,7 @@ end
function scene.keyDown(key) function scene.keyDown(key)
if key=="escape"then if key=="escape"then
if TIME()-lastBackTime<1 then if TIME()-lastBackTime<1 then
NET.signal_quit()
SCN.back() SCN.back()
else else
lastBackTime=TIME() lastBackTime=TIME()