- 78
- 124
- Версия MoonLoader
- .026-beta
Решил перейти с Requests и Effil, на Copas и посыпались проблемы: не получается отправить запрос, пробовал как версию из туториала от FYP, так и разные версии своего кода - итог один.
Пример с кодом от FYP:
Версии библиотек:
Copas - https://github.com/lunarmodules/copas/releases/tag/4.7.0
Coxpcall - https://github.com/keplerproject/coxpcall/releases/tag/v1_17_0
BinaryHeap - https://github.com/Tieske/binaryheap.lua/releases/tag/version_0v4
TimerWheel - https://github.com/Tieske/timerwheel.lua/releases/tag/1.0.2
Lua:
local copas = require 'copas'
local http = require 'copas.http'
--- @param method string
--- @param url string
--- @param data table request body for POST requests
--- @param callback function returns status code, response data, error
function NMLibrary.request(method, url, data, callback)
-- start polling task
if not copas.running then
copas.running = true
lua_thread.create(function()
-- check success of assignment and handle errors if they occur
while not copas.finished() do
local success, exception = copas.step(0)
if success == nil then error(exception) end
wait(0)
end
copas.running = false
end)
end
return copas.addthread(function()
-- catch errors
copas.setErrorHandler(function(err)
print('Error handler: ' .. tostring(err))
callback(nil, nil, err)
end)
print('URL: ' .. tostring(url))
print('Data: ' .. tostring(data))
-- send request
local response, statusCode = http.request(url, data)
print('Response: ' .. tostring(response))
print('Code: ' .. tostring(statusCode))
-- process response
if type(statusCode) == 'number' then
callback(decodeJson(response), statusCode)
else
callback(nil, nil, statusCode)
end
end)
end
[12:10:57.146020] URL: https://arizona.nyashmyash99.ru/files/autocar/update.json
[12:10:57.147019] Data: nil
[12:10:57.173136] Response: nil
[12:10:57.173136] Invalid argument
[12:10:57.147019] Data: nil
[12:10:57.173136] Response: nil
[12:10:57.173136] Invalid argument
Пример с кодом от FYP:
Функция:
function NMLibrary.httpRequest(request, body, handler)
if not copas.running then
copas.running = true
lua_thread.create(function()
wait(0)
while not copas.finished() do
local ok, err = copas.step(0)
if ok == nil then
error(err)
end
wait(0)
end
copas.running = false
end)
end
if handler then
return copas.addthread(function(r, b, h)
copas.setErrorHandler(function(err)
h(nil, err)
end)
h(http.request(r, b))
end, request, body, handler)
else
local results
local thread = copas.addthread(function(r, b)
copas.setErrorHandler(function(err)
results = {nil, err}
end)
results = table.pack(http.request(r, b))
end, request, body)
while coroutine.status(thread) ~= 'dead' do
wait(0)
end
return table.unpack(results)
end
end
Как вызываю:
NMLibrary.httpRequest('https://arizona.nyashmyash99.ru/files/autocar/update.json', nil, function(request, err)
print('FYP: ' .. tostring(request))
print('FYP: ' .. tostring(err))
end)
FYP: nil
FYP: Invalid argument
FYP: Invalid argument
Версии библиотек:
Copas - https://github.com/lunarmodules/copas/releases/tag/4.7.0
Coxpcall - https://github.com/keplerproject/coxpcall/releases/tag/v1_17_0
BinaryHeap - https://github.com/Tieske/binaryheap.lua/releases/tag/version_0v4
TimerWheel - https://github.com/Tieske/timerwheel.lua/releases/tag/1.0.2
Последнее редактирование: