再升级websocket模块
This commit is contained in:
@@ -471,6 +471,7 @@ function love.errorhandler(msg)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
love.threaderror=nil
|
||||||
|
|
||||||
love.draw,love.update=nil--remove default draw/update
|
love.draw,love.update=nil--remove default draw/update
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ local type=type
|
|||||||
local timer=love.timer.getTime
|
local timer=love.timer.getTime
|
||||||
local CHN=love.thread.newChannel()
|
local CHN=love.thread.newChannel()
|
||||||
local CHN_getCount,CHN_push,CHN_pop=CHN.getCount,CHN.push,CHN.pop
|
local CHN_getCount,CHN_push,CHN_pop=CHN.getCount,CHN.push,CHN.pop
|
||||||
|
local TRD=love.thread.newThread("\n")
|
||||||
|
local TRD_isRunning=TRD.isRunning
|
||||||
|
|
||||||
local WS={}
|
local WS={}
|
||||||
local wsList=setmetatable({},{
|
local wsList=setmetatable({},{
|
||||||
@@ -41,6 +43,9 @@ function WS.switchHost(_1,_2,_3)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function WS.connect(name,subPath,body,timeout)
|
function WS.connect(name,subPath,body,timeout)
|
||||||
|
if wsList[name]and wsList[name].thread then
|
||||||
|
wsList[name].thread:release()
|
||||||
|
end
|
||||||
local ws={
|
local ws={
|
||||||
real=true,
|
real=true,
|
||||||
thread=love.thread.newThread('Zframework/websocket_thread.lua'),
|
thread=love.thread.newThread('Zframework/websocket_thread.lua'),
|
||||||
@@ -142,8 +147,8 @@ end
|
|||||||
function WS.update(dt)
|
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 then
|
if ws.real and ws.status~='dead'then
|
||||||
if ws.thread:isRunning()then
|
if TRD_isRunning(ws.thread)then
|
||||||
if CHN_getCount(ws.triggerCHN)==0 then
|
if CHN_getCount(ws.triggerCHN)==0 then
|
||||||
CHN_push(ws.triggerCHN,0)
|
CHN_push(ws.triggerCHN,0)
|
||||||
end
|
end
|
||||||
@@ -173,9 +178,12 @@ function WS.update(dt)
|
|||||||
if ws.alertTimer>0 then ws.alertTimer=ws.alertTimer-dt end
|
if ws.alertTimer>0 then ws.alertTimer=ws.alertTimer-dt end
|
||||||
else
|
else
|
||||||
ws.status='dead'
|
ws.status='dead'
|
||||||
ws.real=false
|
local err=ws.thread:getError()
|
||||||
WS.alert(name)
|
if err then
|
||||||
MES.new('warn',text.wsClose.."线程错误 Thread error")
|
err=err:sub((err:find(":",(err:find(":")or 0)+1)or 0)+1,(err:find("\n")or 0)-1)
|
||||||
|
MES.new('warn',text.wsClose..err)
|
||||||
|
WS.alert(name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ do--Connect
|
|||||||
|
|
||||||
SOCK:settimeout(timeout)
|
SOCK:settimeout(timeout)
|
||||||
local res,err=SOCK:connect(host,port)
|
local res,err=SOCK:connect(host,port)
|
||||||
if err then CHN_push(readCHN,err)return end
|
assert(res,err)
|
||||||
|
|
||||||
--WebSocket handshake
|
--WebSocket handshake
|
||||||
if not body then body=''end
|
if not body then body=''end
|
||||||
@@ -33,7 +33,7 @@ do--Connect
|
|||||||
|
|
||||||
--First line of HTTP
|
--First line of HTTP
|
||||||
res,err=SOCK:receive('*l')
|
res,err=SOCK:receive('*l')
|
||||||
if not res then CHN_push(readCHN,err)return end
|
assert(res,err)
|
||||||
local code,ctLen
|
local code,ctLen
|
||||||
code=res:find(' ')
|
code=res:find(' ')
|
||||||
code=res:sub(code+1,code+3)
|
code=res:sub(code+1,code+3)
|
||||||
@@ -41,7 +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')
|
||||||
if not res then CHN_push(readCHN,err)return end
|
assert(res,err)
|
||||||
if not ctLen and res:find('length')then
|
if not ctLen and res:find('length')then
|
||||||
ctLen=tonumber(res:match('%d+'))
|
ctLen=tonumber(res:match('%d+'))
|
||||||
end
|
end
|
||||||
@@ -53,13 +53,8 @@ do--Connect
|
|||||||
CHN_push(readCHN,'success')
|
CHN_push(readCHN,'success')
|
||||||
else
|
else
|
||||||
res,err=SOCK:receive(ctLen)
|
res,err=SOCK:receive(ctLen)
|
||||||
if not res then
|
res=JSON.decode(assert(res,err))
|
||||||
CHN_push(readCHN,err)
|
error((code or"XXX")..":"..(res and res.reason or"Server Error"))
|
||||||
else
|
|
||||||
res=JSON.decode(res)
|
|
||||||
CHN_push(readCHN,(code or"XXX")..":"..(res and res.reason or"Server Error"))
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -102,7 +97,6 @@ local sendThread=coroutine.wrap(function()
|
|||||||
end
|
end
|
||||||
YIELD()
|
YIELD()
|
||||||
end
|
end
|
||||||
error("break")
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local function _receive(sock,len)
|
local function _receive(sock,len)
|
||||||
@@ -131,7 +125,7 @@ local readThread=coroutine.wrap(function()
|
|||||||
while true do
|
while true do
|
||||||
--Byte 0-1
|
--Byte 0-1
|
||||||
res,err=_receive(SOCK,2)
|
res,err=_receive(SOCK,2)
|
||||||
assert(not err,err)
|
assert(res,err)
|
||||||
|
|
||||||
op=band(byte(res,1),0x0f)
|
op=band(byte(res,1),0x0f)
|
||||||
fin=band(byte(res,1),0x80)==0x80
|
fin=band(byte(res,1),0x80)==0x80
|
||||||
@@ -140,17 +134,17 @@ local readThread=coroutine.wrap(function()
|
|||||||
local length=band(byte(res,2),0x7f)
|
local length=band(byte(res,2),0x7f)
|
||||||
if length==126 then
|
if length==126 then
|
||||||
res,err=_receive(SOCK,2)
|
res,err=_receive(SOCK,2)
|
||||||
assert(not err,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
|
local lenData
|
||||||
lenData,err=_receive(SOCK,8)
|
lenData,err=_receive(SOCK,8)
|
||||||
assert(not err,err)
|
assert(res,err)
|
||||||
local _,_,_,_,_5,_6,_7,_8=byte(lenData,1,8)
|
local _,_,_,_,_5,_6,_7,_8=byte(lenData,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)
|
||||||
assert(not err,err)
|
assert(res,err)
|
||||||
|
|
||||||
--React
|
--React
|
||||||
if op==8 then--8=close
|
if op==8 then--8=close
|
||||||
@@ -178,7 +172,6 @@ local readThread=coroutine.wrap(function()
|
|||||||
end
|
end
|
||||||
YIELD()
|
YIELD()
|
||||||
end
|
end
|
||||||
error("break")
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local success,err
|
local success,err
|
||||||
@@ -194,3 +187,4 @@ end
|
|||||||
SOCK:close()
|
SOCK:close()
|
||||||
CHN_push(readCHN,8)--close
|
CHN_push(readCHN,8)--close
|
||||||
CHN_push(readCHN,err or"Disconnected")
|
CHN_push(readCHN,err or"Disconnected")
|
||||||
|
error()
|
||||||
Reference in New Issue
Block a user