客户端聊天室改用新的数据交换格式

This commit is contained in:
MrZ626
2021-01-13 22:13:10 +08:00
parent 32f9c82ffc
commit 1957370b5c

View File

@@ -16,15 +16,17 @@ local function focusAtTextbox()
end end
local function sendMessage() local function sendMessage()
local W=WIDGET.active.text local W=WIDGET.active.text
if #W.value>0 and wsWrite(W.value)then if #W.value>0 and wsWrite("T"..W.value)then
W.value="" W.value=""
end end
end end
local function pushText(t)
ins(texts,t)
end
local function clearHistory() local function clearHistory()
while #texts>1 do rem(texts)end while #texts>1 do rem(texts)end
scrollPos=1 scrollPos=1
SFX.play("fall") SFX.play("fall")
collectgarbage()
end end
local scene={} local scene={}
@@ -34,9 +36,9 @@ function scene.sceneInit()
remain=false remain=false
if #texts==0 then if #texts==0 then
ins(texts,{COLOR.dG,text.chatStart}) pushText{COLOR.dG,text.chatStart}
elseif #texts>1 and texts[#texts][1]~=COLOR.dG then elseif #texts>1 and texts[#texts][1]~=COLOR.dG then
ins(texts,{COLOR.dG,text.chatHistory}) pushText{COLOR.dG,text.chatHistory}
end end
scrollPos=#texts scrollPos=#texts
TASK.new(focusAtTextbox)--Widgets are not initialized, so active after 1 frame TASK.new(focusAtTextbox)--Widgets are not initialized, so active after 1 frame
@@ -44,7 +46,7 @@ function scene.sceneInit()
BG.set("none") BG.set("none")
end end
function scene.sceneBack() function scene.sceneBack()
wsWrite("/quit") wsWrite("Q")
WSCONN=false WSCONN=false
LOG.print(text.wsDisconnected,"warn") LOG.print(text.wsDisconnected,"warn")
end end
@@ -75,28 +77,24 @@ function scene.keyDown(k)
end end
function scene.socketRead(mes) function scene.socketRead(mes)
if mes:byte()==35 then--system message local cmd=mes:sub(1,1)
local sep=mes:find(":") local args=splitStr(mes:sub(2),":")
local cmd=mes:sub(2,sep-1) if cmd=="J"or cmd=="L"then
local data=mes:sub(sep+1) pushText{
if cmd=="J"or cmd=="L"then COLOR.lR,args[1],
sep=data:find("@") COLOR.dY,args[2].." ",
local num=data:find("#") COLOR.Y,text[cmd=="J"and"chatJoin"or"chatLeave"]
remain=tonumber(data:sub(1,sep-1)) }
ins(texts,{ remain=tonumber(args[3])
COLOR.lR,data:sub(sep+1,num-1), elseif cmd=="T"then
COLOR.dY,data:sub(num).." ", pushText{
COLOR.Y,(cmd=="J"and text.chatJoin or text.chatLeave), COLOR.W,args[1],
}) COLOR.dY,args[2].." ",
end COLOR.sky,args[3]
else--user message }
local sep=mes:find(":") else
local num=mes:find("#") LOG.print("Illegal message: "..mes,30,COLOR.green)
ins(texts,{ return
COLOR.W,mes:sub(1,num-1),
COLOR.dY,mes:sub(num,sep-1).." ",
COLOR.sky,mes:sub(sep+1),
})
end end
if scrollPos==#texts-1 then if scrollPos==#texts-1 then
scrollPos=scrollPos+1 scrollPos=scrollPos+1
@@ -110,7 +108,7 @@ function scene.update(dt)
heartBeatTimer=heartBeatTimer+dt heartBeatTimer=heartBeatTimer+dt
if heartBeatTimer>42 then if heartBeatTimer>42 then
heartBeatTimer=0 heartBeatTimer=0
wsWrite("/ping") wsWrite("P")
end end
end end
function scene.draw() function scene.draw()