mirror of
https://github.com/lxsang/antd-lua-plugin
synced 2025-07-27 03:09:53 +02:00
mimgrating from another repo
This commit is contained in:
62
example-app/os/ws/filestream.lua
Normal file
62
example-app/os/ws/filestream.lua
Normal file
@ -0,0 +1,62 @@
|
||||
auth_or_die("User unauthorized. Please login")
|
||||
if std.ws.enable() then
|
||||
-- read header
|
||||
local streaming = true
|
||||
while streaming do
|
||||
local header = std.ws.header()
|
||||
if header then
|
||||
if header.mask == 0 then
|
||||
print("Data is not masked")
|
||||
std.ws.close(1012)
|
||||
streaming = false
|
||||
elseif header.opcode == std.ws.CLOSE then
|
||||
print("Connection closed")
|
||||
std.ws.close(1000)
|
||||
streaming = false
|
||||
elseif header.opcode == std.ws.BIN then
|
||||
-- read the file
|
||||
local data = std.ws.read(header)
|
||||
local cmd = nil
|
||||
if data then
|
||||
local str = bytes.__tostring(data)
|
||||
local b,e = str:find("^%d+")
|
||||
if(b) then
|
||||
local size = tonumber(str:sub(b,e))
|
||||
local path = require("fs.vfs").ospath(str:sub(e+1))
|
||||
local file = io.open(path, "rb")
|
||||
if file then
|
||||
local sum, len = 0,0
|
||||
repeat
|
||||
local buffer = file:read(size)
|
||||
if buffer then
|
||||
len = len + #buffer
|
||||
cmd = bytes.new(#buffer)
|
||||
for i = 1, #buffer do
|
||||
cmd[i] = buffer:byte(i)
|
||||
end
|
||||
std.ws.write_bytes(cmd)
|
||||
end
|
||||
until not buffer
|
||||
file:close()
|
||||
print("ospath is : "..path)
|
||||
print("length:",len)
|
||||
else
|
||||
print(path.." is not found")
|
||||
std.ws.close(1011)
|
||||
end
|
||||
else
|
||||
std.ws.close(1011)
|
||||
end
|
||||
else
|
||||
std.ws.close(1011)
|
||||
streaming = false
|
||||
end
|
||||
end
|
||||
else
|
||||
streaming = false
|
||||
end
|
||||
end
|
||||
else
|
||||
print("Web socket is not available.")
|
||||
end
|
||||
print("Quit streaming")
|
33
example-app/os/ws/sock.lua
Normal file
33
example-app/os/ws/sock.lua
Normal file
@ -0,0 +1,33 @@
|
||||
if std.ws.enable() then
|
||||
-- read header
|
||||
local streaming = true
|
||||
while streaming do
|
||||
local header = std.ws.header()
|
||||
if header then
|
||||
if header.mask == 0 then
|
||||
print("Data is not masked")
|
||||
std.ws.close(1012)
|
||||
streaming = false
|
||||
elseif header.opcode == std.ws.CLOSE then
|
||||
print("Connection closed")
|
||||
std.ws.close(1000)
|
||||
streaming = false
|
||||
elseif header.opcode == std.ws.BIN then
|
||||
local data = std.ws.read(header)
|
||||
if data then
|
||||
local path = (__ROOT__.."/ws/img%d.jpg"):format(data[0])
|
||||
print("writing : "..path)
|
||||
std.ws.fwrite(path)
|
||||
else
|
||||
std.ws.close(1011)
|
||||
streaming = false
|
||||
end
|
||||
end
|
||||
else
|
||||
streaming = false
|
||||
end
|
||||
end
|
||||
else
|
||||
print("Web socket is not available.")
|
||||
end
|
||||
print("Quit streaming")
|
Reference in New Issue
Block a user