Skip to content
hp-sam edited this page May 30, 2014 · 2 revisions

http

-- 上传
local f = io.open(somebigfilepath, "r")
local writebegin = 111 -- 上传起始位置
local writeend = 222 -- 上传终了位置
local sentoffset = writebegin -- 文件offset
local ret = http.post{
    url = "http://xxx",
    headers = {
        ["Content-Type"] = "application/octet-stream"
    },
    cainfo = base.getGSandbox():getAppSandbox("framework").appPath .. "/cacert.pem",
    capath = base.getGSandbox():getAppSandbox("framework").appPath,
    onsend = function(len)
        if stop then
            return nil, "abort" -- 如果需要终止上传,返回2个值,第一个值为nil, 第二个值为"abort"
        end
        if sentoffset < writeend then
            f:seek("set", sentoffset) -- 置文件起始位置
            local size = math.min(writeend - sentoffset, len)
            sentoffset = sentoffset + size
            return f:read(size) -- 返回需要上传的buffer内容
        else
            return nil -- 如果上传完成,返回nil
        end
    end,
    onprogress = function(downloadtotal, downloadnow, uploadtotal, uploadnow)
        -- 上传操作(POST/PUT) 参考uploadnow数据,下载操作(GET) 参考downloadnow数据,total数据不保证有效,因为有时候并未提供
        print(downloadtotal, downloadnow, uploadtotal, uploadnow)
    end
}

-- 下载
local f = io.open(somedownloadpath, "wb")
local ret = http.get{
    url = "http://xxx",
    onreceive = function(data)
        f:write(data)
        print("receive", #(data))
    end
}
f:close()

if ret.responseCode ~= 200 or ret.error then
    -- 需要判断最终ret.error是不是没有错误,没有错误才能认为somedownloadpath的文件是完整的。
end

box2d

	local box2d = require "box2d"
	local vec2 = box2d.new_vec2(100, 100)
	local world = box2d.new_world(vec2)
	
	local body1 = world:create_body{
		["type"] = "dynamic",
		position = box2d.new_vec2(10, 10)
	}
	local fixture1 = body1:create_fixture{
		shape = box2d.new_shape_circle(10),
		density = 1,
		restitution = 0.5
	}

Clone this wiki locally