mORMot运行在普通VCL程序中

May 24, 2017 20:29 · 272 words · 1 minute read Delphi mORMot

uses 
  SynCommons, SynLZO, SynCrtSock;
type
  TTestServer = class
    protected
      fstr : string;
      fServer: THttpApiServer; //内部为http.sys的实现
      function Process(Ctxt: THttpServerRequest): cardinal;
    public
      constructor Create(const str:string;const path:string);
      destructor Destroy; override;
  end;


{ TTestServer }

constructor TTestServer.Create(const str:string;const path:string);
begin
  fstr := str;
  fServer := THttpApiServer.Create(false);
  fServer.AddUrl(path,'888',false,'+',true);
  fServer.RegisterCompress(CompressSynLZ); //可以启用不同的压缩库
  fServer.OnRequest := Process;
end;

destructor TTestServer.Destroy;
begin
  fServer.Free;
  inherited;
end;

function TTestServer.Process(Ctxt: THttpServerRequest): cardinal;
begin
  Ctxt.OutContentType := JSON_CONTENT_TYPE;// ContentType: application/json; charset=utf-8

  Ctxt.OutContent := ToUTF8(fstr); //转换到UTF8的字符串, 解决中文乱码的问题
  result := 200;
end;

   //可以绑定多个路径, 但是是同一个端口
   TestServer := TTestServer.Create('{"你好":"aaa"}','root') ; 
   TestServer2 := TTestServer.Create('{"你好":"bbb"}','bbb') ;

非常棒!!