Ok ecco un po' di codice allora:
codice:
procedure TCommonHttpServer.lpExecute(loRequestInfo: TIdHTTPRequestInfo; loResponseInfo: TIdHTTPResponseInfo);
var
loObj : TCommonAjaxMethod;
loInputJson : TJSONObject;
loOutputJson : TJSONObject;
loStringStream : TStringStream;
loCompressor : TIdCompressorZLib;
lsErrorString : string;
begin
// AGU Init variables
loInputJson := nil;
loStringStream := nil;
loCompressor := nil;
loObj := nil;
try
if (poMethodList.Count > 0) then
begin
if poMethodList.TryGetValue(loRequestInfo.URI,loObj) then
begin
with loResponseInfo do
begin
CustomHeaders.AddValue('Access-Control-Allow-Origin', '*');
if (loRequestInfo.CommandType = hcOPTION) then
begin
CustomHeaders.AddValue('Access-Control-Allow-Methods', 'GET,POST');
CustomHeaders.AddValue('Access-Control-Allow-Headers', 'Content-Type, Accept');
CustomHeaders.AddValue('Access-Control-Max-Age', '1728000');
end;
ContentEncoding := 'utf-8';
ContentType := 'application/json; charset=utf-8';
loInputJson := TJSONObject(TJSONObject.ParseJSONValue(loRequestInfo.Params.Text));
loOutputJson := loObj(loInputJson);
loStringStream := TStringStream.Create(loOutputJson.ToString);
loCompressor := TIdCompressorZLib.Create(nil);
ContentStream := TMemoryStream.Create;
loCompressor.CompressStream(loStringStream, ContentStream, 9, GZIP_WINBITS, 9, 0);
ContentEncoding := 'gzip';
ContentLength := ContentStream.Size;
end;
end;
end;
except on E : Exception do
end;
if Assigned(loInputJson) then FreeAndNil(loInputJson);
if Assigned(loStringStream) then FreeAndNil(loStringStream);
if Assigned(loCompressor) then FreeAndNil(loCompressor);
if Assigned(loOutputJson) then FreeAndNil(loOutputJson);
end;
Questo è il metodo che viene chiamato all'evento OnCommandGet e OnCommandOther dell'oggetto TIdHTTPServer.
Il mio oggetto poMethodList è una TDictionary contenente nome del metodo da chiamare e metodo da chiamare ( TCommonAjaxMethod ).
Quest'ultimo è così definito:
codice:
TCommonAjaxMethod = function(loJson: TJSONObject): TJSONObject of object;
Le chiamate lato client invece sono così:
codice:
ServerMgrClass.prototype.SendPostRequest = function( lsUrl, lbAsync, lsDataType, loJson, lsMethodName, loOnSuccess, loOnError, loDefaultResult, liTimeout ){
lsUrl = lsUrl == undefined ? this.Url : lsUrl;
if( !loJson ){
loJson = {};
}
loJson.isEnglish = false;
loJson.sessionId = ( loJson.sessionId != undefined ) ? loJson.sessionId : this.SessionId ;
var loResult = loDefaultResult;
if( this.IsAlive ){
with( this ){
$.ajax({
type : "POST",
data : encodeURIComponent( JSON.stringify( loJson ) ),
dataType : lsDataType ? lsDataType : "json",
async : lbAsync,
timeout : ( liTimeout ? liTimeout : 5000 ),
url : lsUrl + lsMethodName,
success : function( data ){
if( loOnSuccess ){
loOnSuccess( data );
}
if( !lbAsync ){
try{
loResult = data;
}
catch( e ){
loResult = null;
}
}
},
error : function(e){
if( loOnError ){
loOnError();
}
}
});
}
}
else{
if( loOnError ){
loOnError();
}
}
return loResult;
}
Mentre quest'ultimo è il timer di watchdog:
codice:
...
...
with( this ){
this.Watchdog = setInterval(function(){
$.ajax( {
type : "POST",
data : JSON.stringify( {} ),
dataType : "json",
async : true,
timeout : 2000,
processData : false,
url : Url + "WatchDog",
success : function( data ){
IsAlive = true;
LastResponse = new Date( data.localTime );
document.getElementById( "indexDivTimeBar" ).innerHTML = LastResponse.format( "dd/mm/yyyy HH:MM:ss" );
},
error : function(){
IsAlive = false;
}
});
}, 1000);
}
...
...