local lanes = require('lanes').configure()
function main()
repeat wait(0) until isSampAvailable()
sampRegisterChatCommand('testt', function()
async_http_request("GET", "https://api.ipify.org/?format=json", {params = {name = "test"}}, function(response)
sampfuncsLog("you ip: "..response.text)
sampAddChatMessage("you ip: "..response.text, -1)
end, function(err) print(err) end)
end)
wait(-1)
end
function async_http_request(method, url, args, resolve, reject)
local request_lane = lanes.gen('*', {package = {path = package.path, cpath = package.cpath}}, function()
local requests = require 'requests'
local ok, result = pcall(requests.request, method, url, args)
if ok then
result.json, result.xml = nil, nil -- cannot be passed through a lane
return true, result
else
return false, result -- return error
end
end)
if not reject then reject = function() end end
lua_thread.create(function()
local lh = request_lane()
while true do
local status = lh.status
if status == 'done' then
local ok, result = lh[1], lh[2]
if ok then resolve(result) else reject(result) end
return
elseif status == 'error' then
return reject(lh[1])
elseif status == 'killed' or status == 'cancelled' then
return reject(status)
end
wait(0)
end
end)
end