Thursday, July 19, 2012

DOR Mobile

Just started exploring Delphi on Rails (DOR). 
  • application/web server based on MVC and REST.
  • dorHTTPClient an easy to use http/https to replace msxml
  • dorWebsocket is a websocket client 
  • dorXMPPClient a jabber client  (you could use to connect DOR servers together )
  • LUA scripting
  • Redis client








WebSocket is very HTML5 wich seems to be the new buzzword. I could think about, instead of Ajax refresh hammering the server every 5 seconds, an WebSocket/message send by the server to the webpage to refresh itself. It's all available within DOR.


DOR Mobile is an example how to use DOR in combination with JQuery mobile.  Inspired by the example Delphi XE2 Boilerplate DataSnap Server and jQueryMobile Client by Embarcadero, I was wondering how much code was needed with DOR to create the same result. Btw Datasnap is not available to Delphi Professional users.


My conclusion: 
- less is more :-)


We first start with an basic DOR server console APP


 unit WebServer;  
 interface  
 uses  
  dorHTTPStub, dorSocketStub;  
 type  
  THTTPConnection = class(THTTPStub)  
  protected  
   function GetPassPhrase: AnsiString; override;  
   procedure ProcessRequest; override;  
  end;  
 implementation  
 uses dorOpenSSL, WinSock;  
 { THTTPConnection }  

 function THTTPConnection.GetPassPhrase: AnsiString;  
 const  
  PASS_PHRASE: AnsiString = 'dc62rtd6fc14ss6df464c2s3s3rt32aed4vh27d3fc321h2vfghv312';  
 begin  
  Result := PASS_PHRASE;  
 end;  

 procedure THTTPConnection.ProcessRequest;  
 begin  
  inherited;  
  if (ErrorCode = 404) and (Params.S['format'] = 'json') then  
  begin  
   Render(Return);  
   ErrorCode := 200;  
  end;  
 end;  

 initialization  
  TSocketServer.CreateServer(83, '0.0.0.0', THTTPConnection);  
 end.  

We have now completed our DOR/Rest Server, after that we have to add our controller:

 unit reverse_controller;  
 interface  
 uses application_controller;  
 type  
  TReverseController = class(TApplicationController)  
  private  
   public  
   procedure GetReverseStr_get(var value:string);  
  end;  
 implementation  
 uses StrUtils;  
 { TReverseController }  

 procedure TReverseController.GetReverseStr_get(var value: string);  
 begin  
  value:=ReverseString(value);  
 end;  

 initialization  
  TReverseController.Register;  
 end.  

That's all fooks, the rest (what's in a name) is HTML/JAVA


First lets create an boilerplate for an mobile interface using: Jquery Mobile


<% function content_for_head() %>  

    <title><DOR Delphi On Rails></title>  
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.css" />  
         <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>'  
     <script type="text/javascript" src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js"></script>  

 <% end %>  

 <% function content_for_body() %>  

   <div data-role="page" id="main" data-theme="a">  
      <div data-role="header" >  
           <h1>Delphi On Rails DOR</h1>  
      </div><!-- /header -->  
      <div data-role="content">       
           <p>Main Page</p>            
           <p>Click "Reverse" button to invoke DOR server method to reverse the content of the edit box</p>  
     <input id="valueField" type="text" value="Aa B Cc" />  
     <a href="#" data-role="button" id="btn_reverse">Reverse</a>  
   </div><!-- /content -->  
   </div><!-- /page -->  

  <% end %>  


Now it's time to add the magic with  Javascript. When the button Reverse is pressed, javascript should be called, and do an  request to our DOR server.

 <script type="text/javascript">  
  $(document).ready(function() {   
 $('#btn_reverse').click (function (){  
  var value = $('#valueField').val();  
  $.get("/reverse/getreversestr.json", {"value": value},   
  function (data) {$('#valueField').val(data.value)}) ;  
   })  
  });  
 </script>