ws库修补
This commit is contained in:
@@ -15,11 +15,8 @@ local byte,char=string.byte,string.char
|
|||||||
local band,bor,bxor=bit.band,bit.bor,bit.bxor
|
local band,bor,bxor=bit.band,bit.bor,bit.bxor
|
||||||
local shl,shr=bit.lshift,bit.rshift
|
local shl,shr=bit.lshift,bit.rshift
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local SOCK=require"socket".tcp()
|
local SOCK=require"socket".tcp()
|
||||||
|
require"Zframework/json"
|
||||||
|
|
||||||
|
|
||||||
local mask_key={1,14,5,14}
|
local mask_key={1,14,5,14}
|
||||||
local function _send(opcode,message)
|
local function _send(opcode,message)
|
||||||
@@ -56,29 +53,47 @@ do--Connect
|
|||||||
local path=sendCHN:demand()
|
local path=sendCHN:demand()
|
||||||
local body=sendCHN:demand()
|
local body=sendCHN:demand()
|
||||||
|
|
||||||
|
SOCK:settimeout(2.6)
|
||||||
local res,err=SOCK:connect(host,port)
|
local res,err=SOCK:connect(host,port)
|
||||||
if res~=1 then error(res,err)end
|
if res then
|
||||||
|
--WebSocket handshake
|
||||||
|
if not body then body=""end
|
||||||
|
SOCK:send(
|
||||||
|
"GET "..path.." HTTP/1.1\r\n"..
|
||||||
|
"Host: "..host..":"..port.."\r\n"..
|
||||||
|
"Connection: Upgrade\r\n"..
|
||||||
|
"Upgrade: websocket\r\n"..
|
||||||
|
"Content-Type: application/json\r\n"..
|
||||||
|
"Content-Length: "..#body.."\r\n"..
|
||||||
|
"Sec-WebSocket-Version: 13\r\n"..
|
||||||
|
"Sec-WebSocket-Key: osT3F7mvlojIvf3/8uIsJQ==\r\n\r\n"..--secKey
|
||||||
|
body
|
||||||
|
)
|
||||||
|
|
||||||
--WebSocket handshake
|
--First line of HTTP
|
||||||
if not body then body=""end
|
local l=SOCK:receive("*l")
|
||||||
SOCK:send(
|
local code=l:find(" "); code=l:sub(code+1,code+3)
|
||||||
"GET "..path.." HTTP/1.1\r\n"..
|
|
||||||
"Host: "..host..":"..port.."\r\n"..
|
if code=="200"then
|
||||||
"Connection: Upgrade\r\n"..
|
readCHN:push("success")
|
||||||
"Upgrade: websocket\r\n"..
|
else
|
||||||
"Content-Type: application/json\r\n"..
|
repeat
|
||||||
"Content-Length: "..#body.."\r\n"..
|
l=SOCK:receive("*l")
|
||||||
"Sec-WebSocket-Version: 13\r\n"..
|
until l==""
|
||||||
"Sec-WebSocket-Key: osT3F7mvlojIvf3/8uIsJQ==\r\n\r\n"..--secKey
|
l=SOCK:receive("*l")
|
||||||
body
|
local reason=json.decode(l)if reason then reason=reason.message end
|
||||||
)
|
readCHN:push(code.."-"..(reason or l))
|
||||||
readCHN:push("success")
|
end
|
||||||
|
else
|
||||||
|
readCHN:push(err)
|
||||||
|
end
|
||||||
|
SOCK:settimeout(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
triggerCHN:demond()
|
triggerCHN:demand()
|
||||||
while sendCHN:getCount()>=2 do
|
while sendCHN:getCount()>=2 do
|
||||||
local op=sendCHN:pop()
|
local op=sendCHN:pop()
|
||||||
local message=sendCHN:pop()
|
local message=sendCHN:pop()
|
||||||
@@ -110,7 +125,15 @@ while true do
|
|||||||
readCHN:push(op)
|
readCHN:push(op)
|
||||||
if op==8 then--close
|
if op==8 then--close
|
||||||
SOCK:close()
|
SOCK:close()
|
||||||
readCHN:push(string.format("%d-%s",shl(byte(res,1),8)+byte(res,2).."-"..res:sub(3,-3)))
|
if res:sub(1,4)=="HTTP"then
|
||||||
|
local code=res:find(" ")
|
||||||
|
code=res:sub(code+1,code+3)
|
||||||
|
local res=res:sub(res:find("\n\n")+1)
|
||||||
|
reason=json.decode(res)if reason then reason=reason.message end
|
||||||
|
readCHN:push(code.."-"..(reason or res))
|
||||||
|
else
|
||||||
|
readCHN:push(string.format("%d-%s",shl(byte(res,1),8)+byte(res,2).."-"..res:sub(3,-3)))
|
||||||
|
end
|
||||||
else
|
else
|
||||||
readCHN:push(res)
|
readCHN:push(res)
|
||||||
end
|
end
|
||||||
@@ -188,10 +211,16 @@ function WS.update()
|
|||||||
for name,ws in next,wsList do
|
for name,ws in next,wsList do
|
||||||
ws.triggerCHN:push(0)
|
ws.triggerCHN:push(0)
|
||||||
if ws.status=="connecting"then
|
if ws.status=="connecting"then
|
||||||
if ws.readCHN:pop()=="success"then
|
local mes=ws.readCHN:pop()
|
||||||
ws.status="running"
|
if mes then
|
||||||
ws.lastPingTime=time
|
if mes=="success"then
|
||||||
ws.lastPongTime=time
|
ws.status="running"
|
||||||
|
ws.lastPingTime=time
|
||||||
|
ws.lastPongTime=time
|
||||||
|
else
|
||||||
|
ws.status="dead"
|
||||||
|
LOG.print(text.wsFailed,"warn")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
elseif time-ws.lastPingTime>ws.pingInterval then
|
elseif time-ws.lastPingTime>ws.pingInterval then
|
||||||
ws.sendCHN:push(9)
|
ws.sendCHN:push(9)
|
||||||
|
|||||||
Reference in New Issue
Block a user