前几天我说了使用delphi-cross-socket 扩展kbmmw 的跨平台支持,今天我说一下使用
kbmMWCrossScoketHttpServerTransport 在linux 下支持 kbmmw 的samrt HTTP service.
本例子基于以前的 。
我们首先在dm 里面放置两个控件。
加入我们的smart http service.
连接linux 运行.
在浏览器里面访问。
在linux 里面查看进程
大家可以看见这个后台进程。
ok, 跨平台就这么任性。
相关代码
program Project2;{ $APPTYPE CONSOLE}{ $R *.res}uses Posix.Unistd, Posix.SysTypes, System.SysUtils, dmp in 'dmp.pas' { dmf: TDataModule}, httpservice in 'httpservice.pas' { kbmMWCustomHTTPSmartService1: TkbmMWCustomHTTPSmartService};procedure daemon;begin dmf:=Tdmf.Create(nil); dmf.kbmmwserver1.AutoRegisterServices; dmf.kbmMWServer1.Active:=True; writeln('service started'); try repeat sleep(10 * 1000); until False; finally dmf.Free; end;end;var pid: pid_t;begin pid := fork; if pid = 0 then begin writeln('starting service'); daemon; end;end.
unit httpservice;// =========================================================================// kbmMW - An advanced and extendable middleware framework.// by Components4Developers (http://www.components4developers.com)//// Service generated by kbmMW service wizard.//// INSTRUCTIONS FOR REGISTRATION/USAGE// -----------------------------------// Please update the uses clause of the datamodule/form the TkbmMWServer is placed on by adding services unit name // to it. Eg.// // uses ...,kbmMWServer,YourServiceUnitName;// // Somewhere in your application, make sure to register the serviceclass to the TkbmMWServer instance.// This can be done by registering the traditional way, or by using auto registration.// // Traditional registration// ------------------------// var// sd:TkbmMWCustomServiceDefinition;// ..// sd:=kbmMWServer1.RegisterService(yourserviceclassname,false);// // Set the last parameter to true if this is the default service.// // // Auto registration// -----------------// Make sure that your service class is tagged with the [kbmMW_Service] attribute.// Then auto register all tagged services:// ..// kbmMWServer1.AutoRegisterServices;// // -----------------------------------------------// // SPECIFIC HTTP SERVICE REGISTRATION INSTRUCTIONS// -----------------------------------------------// Cast the returned service definition object (sd) to a TkbmMWHTTPServiceDefinition. eg:// // var// httpsd:TkbmMWHTTPServiceDefinition;// ..// httpsd:=TkbmMWHTTPServiceDefinition(sd)// httpsd.RootPath[mwhfcHTML]:='/';// httpsd.RootPath[mwhfcImage]:='/images';// httpsd.RootPath[mwhfcJavascript]:='/js';// httpsd.RootPath[mwhfcStyleSheet]:='.';// httpsd.RootPath[mwhfcOther]:='.';// -----------------------------------------------{ $I kbmMW.inc}interfaceuses SysUtils,{ $ifdef LEVEL6} Variants,{ $else} Forms,{ $endif} Classes, kbmMWSecurity, kbmMWServer, kbmMWServiceUtils, kbmMWGlobal, kbmMWCustomHTTPSmartService ,kbmMWHTTPUtils, kbmMWSmartServiceUtils, kbmMWRTTI;type [kbmMW_Service('name:xalionrest, flags:[listed]')] [kbmMW_Rest('path:/xalionrest')] // Access to the service can be limited using the [kbmMW_Auth..] attribute. // [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')] //[kbmMW_HTTP('accept:[get], root:[media:"webfiles", html:"webfiles"]')] TkbmMWCustomHTTPSmartService1 = class(TkbmMWCustomHTTPSmartService) private { Private declarations } protected { Protected declarations } public { Public declarations } // HelloWorld function callable from both a regular client, // due to the optional [kbmMW_Method] attribute, // and from a REST client due to the optional [kbmMW_Rest] attribute. // The access path to the function from a REST client (like a browser)+ // is in this case relative to the services path. // In this example: http://.../xalionhttp/helloworld // Access to the function can be limited using the [kbmMW_Auth..] attribute. // [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')] [kbmMW_Rest('method:get, path:helloworld')] [kbmMW_Method] function HelloWorld:string; [kbmMW_Rest('method:get, path:version')] [kbmMW_Method] function version:string; [kbmMW_Method('EchoString')] // 回应输入的串 [kbmMW_Rest('method:get, path: ["echostring/{AString}","myechostring/{AString}" ]')] [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')] function EchoString([kbmMW_Rest('value: "{AString}"')] const AString:string):string; [kbmMW_Method] [kbmMW_Rest('method:get, path: "cal/addnumbers"')] function AddNumbers([kbmMW_Rest('value: "$arg1", required: true')] const AValue1:integer; [kbmMW_Rest('value: "$arg2", required: true')] const AValue2:integer; [kbmMW_Arg(mwatRemoteLocation)] const ARemoteLocation:string):string; [kbmMW_Rest('method:post, path:postdata')] [kbmMW_Method] function postdata:string; end;implementation{ %CLASSGROUP 'System.Classes.TPersistent'}uses kbmMWExceptions;{ $R *.dfm}// Service definitions.//---------------------function TkbmMWCustomHTTPSmartService1.version: string;begin Result:='{"result":"'+self.Server.Version+':'+TOSversion.ToString +'"}';end;function TkbmMWCustomHTTPSmartService1.AddNumbers(const AValue1, AValue2: integer; const ARemoteLocation: string):string;begin Result:='{"result":"'+(AValue1+AValue2).ToString+'"}';;end;function TkbmMWCustomHTTPSmartService1.EchoString( const AString: string): string;begin result:='{"result":"你好!'+astring+'"}';;end;function TkbmMWCustomHTTPSmartService1.HelloWorld:string;begin Result:='{"result":"Hello world"}';end;