略微优化ws库代码

This commit is contained in:
MrZ626
2021-02-13 20:42:48 +08:00
parent d6037ad15b
commit ab5d6878de

View File

@@ -52,13 +52,12 @@ function _M:connect(host, port, path)
-- debug_print("[handshake] succeed") -- debug_print("[handshake] succeed")
end end
local mask_key = {1, 14, 5, 14}
local function send(SOCK, opcode, message) local function send(SOCK, opcode, message)
local mask_key = {1, 14, 5, 14}
-- message type -- message type
SOCK:send(string.char(bor(0x80, opcode))) SOCK:send(string.char(bor(0x80, opcode)))
if message==nil then if not message then
SOCK:send(string.char(0x80, unpack(mask_key))) SOCK:send(string.char(0x80, unpack(mask_key)))
return 0 return 0
end end
@@ -105,10 +104,10 @@ function _M:read()
-- byte 0-1 -- byte 0-1
local SOCK = self.socket local SOCK = self.socket
local res, err = SOCK:receive(2) local res, err = SOCK:receive(2)
if res==nil then return res, err end if not res then return res, err end
-- local flag_FIN = res:byte()>=0x80
local OPCODE = band(res:byte(), 0x0f) local OPCODE = band(res:byte(), 0x0f)
-- local flag_FIN = res:byte()>=0x80
-- local flag_MASK = res:byte(2)>=0x80 -- local flag_MASK = res:byte(2)>=0x80
-- debug_print("[decode] FIN="..tostring(flag_FIN)..", OPCODE="..OPCODE..", MASK="..tostring(flag_MASK)) -- debug_print("[decode] FIN="..tostring(flag_FIN)..", OPCODE="..OPCODE..", MASK="..tostring(flag_MASK))