Skip to content

Tutorial Creating a Server

José Benedito edited this page May 25, 2014 · 7 revisions

Tutorial - Creating a Server

Create a form with components:

  • TButton Start
  • TButton Stop
  • TMemo for log
  • TCeosServer

Form with components

Set CeosServer1 properties:

CeosServer1 properties

Create a procedure for log:

procedure TForm1.Log(AMsg: string);
begin
  Memo1.lines.add(formatdatetime('hh:nn:ss:zzz',now) + #9 + AMsg);
end;

Assign event OnRequest in CeosServer1:

CeosServer1 events

Code for OnRequest (add fpjson in uses clause):

  AResponse := JSONRPCResult(TJSONString.create('Hello World!'),ARequest.ID);

  Log(format('Response to ID %d',[ARequest.ID]));

Assign CeosServer event OnRequestError:

  AResponse := JSONRPCError(-32100,'Request error.');

  Log(format('Error: %s',[E.message]));

Assign CeosServer1 event OnStart:

  Log('Start...');

Assign CeosServer1 event OnStop:

  Log('Stop...');

Button Start OnClick:

  CeosServer1.Start;

Button Stop OnClick:

  CeosServer1.Stop;

Build and Run!

Server running

Linux

In linux project (ubuntu tested) needs use cthreads unit, by default lazarus add UseCThreads directive:

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}

Thanks Osvaldo T Crispim Filho for the test and tip.

Download

Source code for this tutorial: demoserver.zip

Best regards

JB