-
Notifications
You must be signed in to change notification settings - Fork 13
Tutorial Creating a Server
José Benedito edited this page May 25, 2014
·
7 revisions
Create a form with components:
- TButton Start
- TButton Stop
- TMemo for log
- TCeosServer
Set 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:
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!
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