把所有网络操作抽象到NET中

This commit is contained in:
MrZ626
2021-03-28 21:08:15 +08:00
parent 35ac6eacbc
commit 4222fff2e3
8 changed files with 105 additions and 73 deletions

View File

@@ -82,7 +82,7 @@ function scene.keyDown(key)
TEXT.show(text.needUpdate,640,450,60,"flicker")
SFX.play("finesseError")
else
WS.send("user",JSON.encode{action=0})
NET.getAccessToken()
end
else
SCN.go("login")

View File

@@ -3,12 +3,12 @@ local data=love.data
local textBox=WIDGET.newTextBox{name="texts",x=40,y=50,w=1200,h=430}
local remain--People in chat room
local heartBeatTimer
local escapeTimer=0
local function sendMessage()
local W=WIDGET.active.input
if #W.value>0 and WS.send("chat","T"..data.encode("string","base64",W.value))then
if #W.value>0 then
NET.sendChatMes(W.value)
W.value=""
end
end
@@ -16,7 +16,6 @@ end
local scene={}
function scene.sceneInit()
heartBeatTimer=0
remain=false
local texts=textBox.texts
@@ -30,7 +29,7 @@ function scene.sceneInit()
BG.set("none")
end
function scene.sceneBack()
WS.send("chat","Q")
NET.quitChat()
LOG.print(text.wsDisconnected,"warn")
end
@@ -80,13 +79,6 @@ function scene.socketRead(mes)
end
end
function scene.update(dt)
heartBeatTimer=heartBeatTimer+dt
if heartBeatTimer>42 then
heartBeatTimer=0
WS.send("chat","P")
end
end
function scene.draw()
setFont(25)
gc.setColor(1,1,1)

View File

@@ -20,7 +20,6 @@ end
local playerInitialized
local playing
local heartBeatTimer
local lastUpstreamTime
local upstreamProgress
local lastBackTime=0
@@ -30,7 +29,7 @@ local touchMoveLastFrame=false
local scene={}
function scene.sceneBack()
WS.send("play","Q")
NET.signal_quit()
LOG.print(text.wsDisconnected,"warn")
love.keyboard.setKeyRepeat(true)
end
@@ -107,7 +106,7 @@ function scene.keyDown(key)
end
elseif key=="space"then
if not PLAYERS[1].ready then
WS.send("play","R")
NET.signal_ready()
end
end
end
@@ -262,14 +261,7 @@ function scene.update(dt)
local GAME=GAME
if WS.status("play")~="running"and not SCN.swapping then SCN.back()end
if not playing then
heartBeatTimer=heartBeatTimer+dt
if heartBeatTimer>42 then
heartBeatTimer=0
WS.send("play","P")
end
return
end
if not playing then return end
touchMoveLastFrame=false
updateVirtualkey()
@@ -289,7 +281,7 @@ function scene.update(dt)
local stream
stream,upstreamProgress=dumpRecording(GAME.rep,upstreamProgress)
if #stream>0 then
WS.send("stream",data.encode("string","base64",stream))
NET.uploadRecStream(stream)
else
ins(GAME.rep,GAME.frame)
ins(GAME.rep,0)

View File

@@ -9,24 +9,7 @@ local lastCreateRoomTime=0
local function fresh()
lastfreshTime=TIME()
rooms=nil
WS.send("play","/play",JSON.encode{
action=0,
data={
type=nil,
begin=0,
count=10,
}
})
end
local function enterRoom(roomID,password)
WS.send("play","/play",JSON.encode{
action=2,
data={
rid=roomID,
conf=dumpBasicConfig(),
password=password,
}
})
NET.freshRoom()
end
local scene={}
@@ -48,15 +31,7 @@ function scene.keyDown(k)
end
elseif k=="n"then
if TIME()-lastCreateRoomTime>26 then
WS.send("play",JSON.encode{
action=1,
data={
type=nil,
name=(USER.name or"???").."'s room",
password=nil,
conf=dumpBasicConfig(),
}
})
NET.createRoom()
lastCreateRoomTime=TIME()
else
LOG.print(text.createRoomTooFast,"warn")
@@ -83,7 +58,7 @@ function scene.keyDown(k)
LOG.print("Can't enter private room now")
return
end
enterRoom(rooms[selected].id)
NET.enterRoom(rooms[selected].id)--,password
end
end
end