- 152
- 56
- Версия MoonLoader
- .027.0-preview
Пишу Lua скрипт, он шлёт события на сервер через HTTP. Всё вроде норм, но если в чате начинается жёсткий спам (например семейные новости), то игра просто крашится.
Есть ощущение, что я как-то криво сделал асинхронность. Использую effil.thread и requests. Когда запросов много — что-то ломается.
Как лучше сделать асинхронку, чтобы игра не лагала и не падала?
Есть ощущение, что я как-то криво сделал асинхронность. Использую effil.thread и requests. Когда запросов много — что-то ломается.
Вот куски кода::
local function http_post(url, body)
local request_thread = effil.thread(function(url, body, timeout)
local requests = require 'requests'
local result, response = pcall(requests.request, "POST", url, {
headers = {
["Content-Type"] = "application/json",
["Content-Length"] = tostring(#body)
},
data = body,
timeout = timeout
})
if result then
return {
success = true,
code = response.status_code,
text = response.text,
headers = response.headers
}
else
return {
success = false,
error = response
}
end
end)(url, body, HTTP_TIMEOUT_MS/1000)
lua_thread.create(function()
while true do
local status, err = request_thread:status()
if not err then
if status == 'completed' then
local result = request_thread:get()
if result.success then
-- обработка ответа
else
print("HTTP request failed: "..tostring(result.error))
end
return
elseif status == 'canceled' then
return
end
else
print("Thread error: "..tostring(err))
return
end
wait(0)
end
end)
end
А вызываю так::
local function notifyServer(payload)
payload.version = VERSION
local json_payload = json.encode(payload)
http_post(SERVER_URL, json_payload)
end
Как лучше сделать асинхронку, чтобы игра не лагала и не падала?