修复ws库分包消息解析错误

This commit is contained in:
MrZ626
2021-04-09 19:35:18 +08:00
parent 48ce3a3d8f
commit e46a28681d

View File

@@ -110,6 +110,8 @@ local _send do
end end
end end
end end
local res,err
local op,fin
local length local length
local lBuffer=""--Long multi-data buffer local lBuffer=""--Long multi-data buffer
local UFF--Un-finished-frame mode local UFF--Un-finished-frame mode
@@ -125,25 +127,13 @@ while true do--Running
--Read --Read
while true do while true do
if UFF then--UNF process if not UFF then--UNF process
local s,e,p=SOCK:receive(length)
if s then
sBuffer=sBuffer..s
UFF=false
elseif p then
sBuffer=sBuffer..p
length=length-#p
if length==0 then
UFF=false
end
end
else
--Byte 0-1 --Byte 0-1
local res,err=SOCK:receive(2) res,err=SOCK:receive(2)
if err then break end if err then break end
local op=band(byte(res,1),0x0f) op=band(byte(res,1),0x0f)
local fin=band(byte(res,1),0x80)==0x80 fin=band(byte(res,1),0x80)==0x80
--Calculating data length --Calculating data length
length=band(byte(res,2),0x7f) length=band(byte(res,2),0x7f)
@@ -171,36 +161,54 @@ while true do--Running
else else
res="" res=""
end end
else
--React local s,e,p=SOCK:receive(length)
if op==8 then--8=close if s then
readCHN:push(op) ]]..(debug==1 and""or"--")..[[print(("%s<%d>:%s"):format(threadName,length,s))
SOCK:close() sBuffer=sBuffer..s
if type(res)=="string"then length=length-#s
res=JSON.decode(res) elseif p then
readCHN:push(res and res.reason or"WS Error") ]]..(debug==1 and""or"--")..[[print(("%s<%d>:%s"):format(threadName,length,p))
else sBuffer=sBuffer..p
readCHN:push("WS Error") length=length-#p
end end
elseif op==0 then--0=continue if length==0 then
lBuffer=lBuffer..res res,sBuffer=sBuffer,""
if fin then UFF=false
]]..(debug==2 and""or"--")..[[print("FIN=1 (c")
readCHN:push(lBuffer)
lBuffer=""
else
]]..(debug==2 and""or"--")..[[print("FIN=0 (c")
end
else else
readCHN:push(op) break
if fin then end
]]..(debug==2 and""or"--")..[[print("OP: "..op.."\tFIN=1") end
readCHN:push(res) ]]..(debug==1 and""or"--")..[[print(("%s<[%d]>:%s"):format(threadName,#res,res))
else
]]..(debug==2 and""or"--")..[[print("OP: "..op.."\tFIN=0") --React
sBuffer=res if op==8 then--8=close
]]..(debug==2 and""or"--")..[[print("START pack: "..res) readCHN:push(op)
end SOCK:close()
if type(res)=="string"then
res=JSON.decode(res)
readCHN:push(res and res.reason or"WS Error")
else
readCHN:push("WS Error")
end
elseif op==0 then--0=continue
lBuffer=lBuffer..res
if fin then
]]..(debug==2 and""or"--")..[[print("FIN=1 (c")
readCHN:push(lBuffer)
lBuffer=""
else
]]..(debug==2 and""or"--")..[[print("FIN=0 (c")
end
else
readCHN:push(op)
if fin then
]]..(debug==2 and""or"--")..[[print("OP: "..op.."\tFIN=1")
readCHN:push(res)
else
]]..(debug==2 and""or"--")..[[print("OP: "..op.."\tFIN=0")
sBuffer=res
]]..(debug==2 and""or"--")..[[print("START pack: "..res)
end end
end end
end end