From 069b547e581fc21b9331aa38ca54848c20f5338d Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Fri, 5 Mar 2021 01:00:22 +0800 Subject: [PATCH] =?UTF-8?q?ws=E5=BA=93=E4=BF=AE=E8=A1=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework/websocket.lua | 79 +++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 25 deletions(-) diff --git a/Zframework/websocket.lua b/Zframework/websocket.lua index 5ba1039a..00040a2a 100644 --- a/Zframework/websocket.lua +++ b/Zframework/websocket.lua @@ -15,11 +15,8 @@ local byte,char=string.byte,string.char local band,bor,bxor=bit.band,bit.bor,bit.bxor local shl,shr=bit.lshift,bit.rshift - - local SOCK=require"socket".tcp() - - +require"Zframework/json" local mask_key={1,14,5,14} local function _send(opcode,message) @@ -56,29 +53,47 @@ do--Connect local path=sendCHN:demand() local body=sendCHN:demand() + SOCK:settimeout(2.6) 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 - 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 - ) - readCHN:push("success") + --First line of HTTP + local l=SOCK:receive("*l") + local code=l:find(" "); code=l:sub(code+1,code+3) + + if code=="200"then + readCHN:push("success") + else + repeat + l=SOCK:receive("*l") + until l=="" + l=SOCK:receive("*l") + local reason=json.decode(l)if reason then reason=reason.message end + readCHN:push(code.."-"..(reason or l)) + end + else + readCHN:push(err) + end + SOCK:settimeout(0) end while true do - triggerCHN:demond() + triggerCHN:demand() while sendCHN:getCount()>=2 do local op=sendCHN:pop() local message=sendCHN:pop() @@ -110,7 +125,15 @@ while true do readCHN:push(op) if op==8 then--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 readCHN:push(res) end @@ -188,10 +211,16 @@ function WS.update() for name,ws in next,wsList do ws.triggerCHN:push(0) if ws.status=="connecting"then - if ws.readCHN:pop()=="success"then - ws.status="running" - ws.lastPingTime=time - ws.lastPongTime=time + local mes=ws.readCHN:pop() + if mes then + if mes=="success"then + ws.status="running" + ws.lastPingTime=time + ws.lastPongTime=time + else + ws.status="dead" + LOG.print(text.wsFailed,"warn") + end end elseif time-ws.lastPingTime>ws.pingInterval then ws.sendCHN:push(9)