Jump to content

Help please fetchRemote


ilnaz

Recommended Posts

How to improve the code so that it works quickly and without delay?

This script works well when there are few players, but when more than 20 people come in, it starts to load for a long time. A very long time. It can load for hours.

 

CLIENT ↓↓

triggerServerEvent("onSearchRequest",localPlayer, 'text_search')

CLIENT ↑↑

 

SERVER ↓↓

addEvent("onSearchRequest", true)
addEventHandler("onSearchRequest",getRootElement(),function(txt)
    fetchRemote( "http://5music.online/?song="..(txt:gsub(' ', '+')),onSongReturn,'',true,client)
end)

 

function onSongReturn(data,errno,player)
    if player and isElement(player) then
        local str1 = string.find(data,"<ul>")
        local _,str2 = string.find(data,"</ul>")
        if str1 ~= nil then
            local data = string.sub(data,str1,str2)
            triggerClientEvent(player,"onSongReceived", player, data)
        end
    end
end

UPGRADE CODE PLEASE

Link to comment
  • Moderators

@ilnaz

 

 

Re-used data, with a buffer.

local songBuffer = {}
addEvent("onSearchRequest", true)
addEventHandler("onSearchRequest",getRootElement(),function(txt)
	local buffer = songBuffer[txt]
	if buffer then
		onSongReturn(buffer, 0, player)
	else
		fetchRemote( "http://5music.online/?song="..(txt:gsub(' ', '+')),onSongReturn,'',true,client, txt)
	end
end)

 

function onSongReturn(data, errno, player, txt)
	if errno == 0 and txt then
		songBuffer[txt] = data
	end
    if player and isElement(player) then
        local str1 = string.find(data,"<ul>")
        local _,str2 = string.find(data,"</ul>")
        if str1 ~= nil then
            local data = string.sub(data,str1,str2)
            triggerClientEvent(player,"onSongReceived", player, data)
        end
    end
end 

-- clear buffer
setTimer(function () songBuffer = {} end, 1000 * 60 * 60 * 4, 0)

 

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...