Add nui message chunking

This commit is contained in:
Jérémie N'gadi
2018-03-15 12:10:03 +01:00
parent 00201edba0
commit f3e15cabdb
3 changed files with 60 additions and 0 deletions
+2
View File
@@ -33,6 +33,7 @@ client_scripts {
'client/common.lua',
'client/entityiter.lua',
'client/functions.lua',
'client/wrapper.lua',
'client/main.lua'
}
@@ -46,6 +47,7 @@ files {
'html/css/app.css',
'html/js/mustache.min.js',
'html/js/wrapper.js',
'html/js/app.js',
'html/fonts/pdown.ttf',
+16
View File
@@ -0,0 +1,16 @@
local Chunks = {}
RegisterNUICallback('__chunk', function(data, cb)
Chunks[data.id] = Chunks[data.id] or ''
Chunks[data.id] = Chunks[data.id] .. data.chunk
if data['end'] then
local msg = json.decode(Chunks[data.id])
TriggerEvent(GetCurrentResourceName() .. ':message:' .. data.__type, msg)
Chunks[data.id] = nil
end
cb('')
end)
+42
View File
@@ -0,0 +1,42 @@
(() => {
let ESXWrapper = {};
ESXWrapper.MessageSize = 1024;
ESXWrapper.messageId = 0;
window.SendMessage = function(namespace, type, msg) {
ESXWrapper.messageId = (ESXWrapper.messageId < 65535) ? ESXWrapper.messageId + 1 : 0;
const str = JSON.stringify(msg);
for(let i=0; i<str.length; i++) {
let count = 0;
let chunk = '';
while(count < ESXWrapper.MessageSize && i<str.length) {
chunk += str[i];
count++;
i++;
}
i--;
const data = {
__type : type,
id : ESXWrapper.messageId,
chunk : chunk
}
if(i == str.length - 1)
data.end = true;
$.post('http://' + namespace + '/__chunk', JSON.stringify(data));
}
}
})()