整理ws模块代码

This commit is contained in:
MrZ_26
2024-08-13 21:54:11 +08:00
parent 5f5dd48ee8
commit 90f41a20a3
2 changed files with 10 additions and 6 deletions

View File

@@ -8,8 +8,6 @@ local path=''
local type=type local type=type
local timer=love.timer.getTime local timer=love.timer.getTime
local TRD=love.thread.newThread("\n")
local TRD_isRunning=TRD.isRunning
local WS={} local WS={}
local wsList=setmetatable({},{ local wsList=setmetatable({},{
@@ -151,7 +149,7 @@ function WS.update(dt)
local time=timer() local time=timer()
for name,ws in next,wsList do for name,ws in next,wsList do
if ws.real and ws.status~='dead' then if ws.real and ws.status~='dead' then
if TRD_isRunning(ws.thread) then if ws.thread:isRunning() then
if ws.triggerCHN:getCount()==0 then if ws.triggerCHN:getCount()==0 then
ws.triggerCHN:push(0) ws.triggerCHN:push(0)
end end

View File

@@ -15,6 +15,7 @@ do-- Connect
SOCK:settimeout(timeout) SOCK:settimeout(timeout)
local res,err=SOCK:connect(host,port) local res,err=SOCK:connect(host,port)
-- print('C0',res,err)
assert(res,err) assert(res,err)
-- WebSocket handshake -- WebSocket handshake
@@ -31,6 +32,7 @@ do-- Connect
-- First line of HTTP -- First line of HTTP
res,err=SOCK:receive('*l') res,err=SOCK:receive('*l')
-- print('C',res,err)
assert(res,err) assert(res,err)
local code,ctLen local code,ctLen
code=res:find(' ') code=res:find(' ')
@@ -39,6 +41,7 @@ do-- Connect
-- Get body length from headers and remove headers -- Get body length from headers and remove headers
repeat repeat
res,err=SOCK:receive('*l') res,err=SOCK:receive('*l')
-- print('H',res,err)
assert(res,err) assert(res,err)
if not ctLen and res:find('content-length') then if not ctLen and res:find('content-length') then
ctLen=tonumber(res:match('%d+')) or 0 ctLen=tonumber(res:match('%d+')) or 0
@@ -53,6 +56,7 @@ do-- Connect
-- Content(?) -- Content(?)
if ctLen then if ctLen then
res,err=SOCK:receive(ctLen) res,err=SOCK:receive(ctLen)
-- print('R',res,err)
if code~='101' then if code~='101' then
res=JSON.decode(assert(res,err)) res=JSON.decode(assert(res,err))
error((code or "XXX")..":"..(res and res.reason or "Server Error")) error((code or "XXX")..":"..(res and res.reason or "Server Error"))
@@ -140,10 +144,10 @@ local readThread=coroutine.wrap(function()
assert(res,err) assert(res,err)
length=shl(byte(res,1),8)+byte(res,2) length=shl(byte(res,1),8)+byte(res,2)
elseif length==127 then elseif length==127 then
local lenData -- 'res' is 'lenData' here
lenData,err=_receive(SOCK,8) res,err=_receive(SOCK,8)
assert(res,err) assert(res,err)
local _,_,_,_,_5,_6,_7,_8=byte(lenData,1,8) local _,_,_,_,_5,_6,_7,_8=byte(res,1,8)
length=shl(_5,24)+shl(_6,16)+shl(_7,8)+_8 length=shl(_5,24)+shl(_6,16)+shl(_7,8)+_8
end end
res,err=_receive(SOCK,length) res,err=_receive(SOCK,length)
@@ -162,12 +166,14 @@ local readThread=coroutine.wrap(function()
lBuffer=lBuffer..res lBuffer=lBuffer..res
if fin then if fin then
CHN_push(readCHN,lBuffer) CHN_push(readCHN,lBuffer)
-- print('M',lBuffer)
lBuffer="" lBuffer=""
end end
else else
CHN_push(readCHN,op) CHN_push(readCHN,op)
if fin then if fin then
CHN_push(readCHN,res) CHN_push(readCHN,res)
-- print('S',res)
lBuffer="" lBuffer=""
else else
lBuffer=res lBuffer=res