From 101636a60a0d8619379eef6e7b6674e050bfb0ed Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Tue, 30 Mar 2021 01:16:28 +0800 Subject: [PATCH] =?UTF-8?q?ws=E5=BA=93=E5=86=8D=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework/websocket.lua | 65 +++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/Zframework/websocket.lua b/Zframework/websocket.lua index 84ec954b..ea0b52ea 100644 --- a/Zframework/websocket.lua +++ b/Zframework/websocket.lua @@ -93,7 +93,7 @@ do--Connect else readCHN:push(err) end - SOCK:settimeout(0) + SOCK:settimeout(6.26) end @@ -125,31 +125,54 @@ while true do--Running end --Receive data - res=SOCK:receive(length) + res="" + while length>0 do + local t=SOCK:receive(length) + if t then + res=res..t + length=length-#t + else--Time out! + res=false + break + end + end --React - if op==8 then--8=close - readCHN:push(op) - SOCK:close() - if type(res)=="string"then - local reason=JSON.decode(res) - readCHN:push(reason and reason.message or"Server Error") + if res then + if op==8 then--8=close + readCHN:push(op) + SOCK:close() + if type(res)=="string"then + local reason=JSON.decode(res) + readCHN:push(reason and reason.message or"Server Error") + else + readCHN:push("Server Error") + end + elseif op==0 then--0=continue + buffer=buffer..res + if fin then + -- print("FIN=1 (c") + readCHN:push(buffer) + buffer="" + else + -- print("FIN=0 (c") + end else - readCHN:push("Server Error") - end - elseif op==0 then - buffer=buffer..res - if fin then - readCHN:push(buffer) - buffer="" + readCHN:push(op) + if fin then + -- print("OP: "..op.."\tFIN=1") + readCHN:push(res) + else + -- print("OP: "..op.."\tFIN=0") + buffer=res + -- print("START pack: "..res) + end end else - readCHN:push(op) - if fin then - readCHN:push(res) - else - buffer=res - end + --TIMEOUT + SOCK:close() + readCHN:push(8) + readCHN:push("WS time out") end end end