Created
June 7, 2016 21:28
-
-
Save comboy/f6d7084fa3740cda3f3461d2c5c89a88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local band = bit.band | |
local bor = bit.bor | |
local rshift = bit.rshift | |
local char = string.char | |
local payload = _G.do_input.payload | |
local opcode = _G.do_input.opcode | |
opcode = opcode or 2 | |
assert(type(opcode) == "number", "opcode must be number") | |
assert(type(payload) == "string", "payload must be string") | |
local len = #payload | |
local head = char( | |
bor(0x80, opcode), | |
bor(len < 126 and bor(0x80, len) or len < 0x10000 and 254 or 255) | |
) | |
if len >= 0x10000 then | |
head = head .. char( | |
0,0,0,0, -- 32 bit length is plenty, assume zero for rest | |
band(rshift(len, 24), 0xff), | |
band(rshift(len, 16), 0xff), | |
band(rshift(len, 8), 0xff), | |
band(len, 0xff) | |
) | |
elseif len >= 126 then | |
head = head .. char(band(rshift(len, 8), 0xff), band(len, 0xff)) | |
end | |
head = head .. char(0,0,0,0) | |
return head .. payload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment