function ajaxProcess(data, textStatus, jqXHR) { var key,items,html; if (data.moveTo) { window.location.replace(data.moveTo); } else if (data.errorMsg) { showMsg(data.errorMsg); } else { if (data.elmHtmList) for (key in data.elmHtmList) $('#'+key).html(data.elmHtmList[key]); if (data.edtValList) for (key in data.edtValList) $('#'+key).val(data.edtValList[key]); if (data.cmbOptList) for (key in data.cmbOptList){ items=data.cmbOptList[key]; html=''; $.each(items,function(i, value){ if (typeof(value)==='object') html+='<option value="'+value.value+'">'+value.name+'</option>'; else html+='<option value="'+value+'">'+value+'</option>'; }); $('#'+key).html(html); } if (data.script) eval(data.script); } }
function showMsg(info) { $('#dlgInfoText').html(info); ShowObject('dlgInfo', 1); } </script>
第一步:控制SessionID procedure TAppWebModule.WebModuleBeforeDispatch(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); begin FSessionID := Request.CookieFields.Values[cstCookieSessionID]; if Length(FSessionID) = 0 then StartSession else begin ...... end; end;
function GetRandomString(NumChar: UInt32): string; const CharMap = 'qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM1234567890'; { Do not Localize } MaxChar: UInt32 = Length(CharMap) - 1; var i: Integer; begin randomize; SetLength(Result, NumChar); for i := 1 to NumChar do begin Result[i] := CharMap[Random(MaxChar) + 1]; end; end;
procedure TAppWebModule.StartSession; var LCookie: TStringList; p: TPooledStoredProc; begin p := NewStoredProc('dbo.appSessionExist', cstdbSecondary); while True do begin FSessionID := GetRandomString(15); p.ParamByName('@SId').AsString := FSessionID; p.ExecProc; if not p.ParamByName('@Exist').AsBoolean then break; end; p.Free; LCookie := TStringList.Create; LCookie.Add(cstCookieSessionID + '=' + FSessionID); Response.SetCookieField(LCookie, '', '', {$IFDEF ISAPI} -1{$ELSE}0{$ENDIF}, False); FreeAndNil(LCookie); end;
function TAppWebModule.GetSession(const AKey: string): string; var p: TPooledStoredProc; begin p := NewStoredProc('dbo.appSessionGet', cstdbSecondary); p.ParamByName('@SID').AsString := SessionID; p.ParamByName('@SKey').AsString := AKey; p.ExecProc; if p.ParamByName('@SVal').IsNull then Result := '' else Result := p.ParamByName('@SVal').AsString; p.Free; end;
procedure TAppWebModule.SetSession(const AKey, Value: string); var p: TPooledStoredProc; begin p := NewStoredProc('dbo.appSessionSet', cstdbSecondary); p.ParamByName('@SID').AsString := SessionID; p.ParamByName('@SKey').AsString := AKey; p.ParamByName('@SVal').AsString := Value; p.ExecProc; p.Free; end;
Session数据全部以String保存,要读写对象,通过ToJSon/FromJSon转换 下面是SQL Server数据库上的定义: IF EXISTS(SELECT 1 FROM sysobjects WHERE name='AppSessions' AND type='U' AND uid=user_id('dbo')) DROP TABLE dbo.AppSessions GO CREATE TABLE dbo.AppSessions( SId VARCHAR(15) NOT NULL, SKey VARCHAR(80) NOT NULL, SVal VARCHAR(7168) NULL, SExp DATETIME NULL, CONSTRAINT PK_AppSessions PRIMARY KEY(SId,SKey) ) GO
IF EXISTS(SELECT 1 FROM sysobjects WHERE name='appSessionExist' AND type='P' AND uid=user_id('dbo')) DROP PROCEDURE dbo.appSessionExist GO CREATE PROCEDURE dbo.appSessionExist @SId VARCHAR(15), @Exist BIT OUTPUT WITH ENCRYPTION AS SET NOCOUNT ON SET @Exist=0 IF EXISTS(SELECT 1 FROM dbo.AppSessions WHERE SId=@SId) SET @Exist=1 SET NOCOUNT OFF GO
IF EXISTS(SELECT 1 FROM sysobjects WHERE name='appSessionGet' AND type='P' AND uid=user_id('dbo')) DROP PROCEDURE dbo.appSessionGet GO CREATE PROCEDURE dbo.appSessionGet @SId VARCHAR(15), @SKey VARCHAR(80), @SVal VARCHAR(7168) OUTPUT WITH ENCRYPTION AS SET NOCOUNT ON DECLARE @Expire DATETIME SELECT @SVal=SVal,@Expire=SExp FROM dbo.AppSessions WHERE SId=@SId AND SKey=@SKey IF @Expire<GETDATE() BEGIN SET @SVal=NULL DELETE dbo.AppSessions WHERE SId=@SId END ELSE IF @Expire>=GETDATE() UPDATE dbo.AppSessions SET SExp=@Expire+1 WHERE SId=@SId SET NOCOUNT OFF GO
IF EXISTS(SELECT 1 FROM sysobjects WHERE name='appSessionSet' AND type='P' AND uid=user_id('dbo')) DROP PROCEDURE dbo.appSessionSet GO CREATE PROCEDURE dbo.appSessionSet @SId VARCHAR(15), @SKey VARCHAR(80), @SVal VARCHAR(7168) WITH ENCRYPTION AS SET NOCOUNT ON DECLARE @Expire DATETIME SET @Expire=GETDATE()+1 IF EXISTS(SELECT 1 FROM dbo.AppSessions WHERE SId=@SId AND SKey=@SKey) BEGIN UPDATE dbo.appSessions SET SVal=@SVal WHERE SId=@SId AND SKey=@SKey UPDATE dbo.AppSessions SET SExp=@Expire WHERE SId=@SId END ELSE INSERT INTO dbo.AppSessions VALUES(@SId,@SKey,@SVal,@Expire) SET NOCOUNT OFF GO
----------------------------------------------
-
sgcWebSockets is a complete package providing access to WebSockets protocol, allowing to create WebSockets Servers, Intraweb Clients or WebSocket Clients in VCL, FreePascal and Firemonkey applications.
Fully functional multithreaded WebSocket server according to RFC 6455.
Supports Firemonkey (Windows and MacOS).
Supports NEXTGEN Compiler (IOS and Android Support).
Supports Lazarus / FreePascal.
Supports CBuilder.
Supports Chrome, Firefox, Safari, Opera and Internet Explorer (including iPhone, iPad and iPod)
Supports C#.NET using compiled library (for Windows 32 and 64 bits).
Multiple Threads Support
Supports Message Compression using PerMessage_Deflate extension RFC 7692.
Supports Text and Binary Messages.
Supports Server and Client Authentication.
Server component providing WebSocket and HTTP connections through the same port.
Proxy Server component allowing to Web Browsers to connect to any TCP server.
Load Balancing Server.
Client WebSocket supports connections through Socket.IO Servers.
FallBack support through Adobe Flash for old Web Browsers like Internet Explorer from 6 to 9.
Supports Server-Sent Events (Push Notifications) over HTTP Protocol.
WatchDog and HeartBeat built-in support.
Client WebSocket supports connections through HTTP Proxy Servers and SOCKS Proxy Servers.
Built-in sub-protocols: JSON-RPC 2.0, Dataset, WebRTC and WAMP
Support for JSON parsers: Delphi JSON and XSuperObject.
Built-in Javascript libraries to support browser clients.
Easy to setup
Javascript Events for a full control
Async Events using Ajax
SSL/TLS Support for Server / Client Components (OpenSSL libraries required).
Main components available are:
TsgcWebSocketServer: Non-visual component, it's used to manage client threaded connections. Supports RFCC 6455.
TsgcWebSocketHTTPServer: Non-visual component, it's used to manage client threaded connections. Supports RFCC 6455. Supports HTTP Requests using an unique port for WebSocket and HTTP Connections.
TsgcWebSocketLoadBalancerServer: Non-visual component, it's used to distribute messages across several back-up servers.
TsgcWebSocketClient: Non-visual component, used to establish a WebSocket connection with a WebSocket server.
TsgcWebsocketProxyServer: Non-visual component, used to translate websocket connections to normal TCP connections.
TsgcIWWebSocketClient: Non-visual component, used on Intraweb forms to establish a WebSocket connection with a WebSocket Server.
You can use WebSockets too, using sgcWebSockets.dll, modules available:
@c5soft 这个你试过了吗?开源…… DelphiMVCFramework is a powerful RESTful framework used also for website development https://github.com/danieleteti/delphimvcframework
DMVCFramework features •RESTful (RMM Level 3) compliant •Fancy URL with parameter mappings •Server side generated pages using Mustache templates •Messaging extension using STOMP and Apache ActiveMQ or Apache Apollo (beta) •Can be used in load balanced environment using Redis or MySQL as state server •Integrated RESTClient •Works with DelphiXE3 or better •Integrated Logging System •It is really simple to use. You can be productive in minutes!
Here's the DMVCFramework Developers Guide https://danieleteti.gitbooks.io/delphimvcframework/content/
If you need support, there is the official facebook group with more than 600 users https://www.facebook.com/groups/delphimvcframework/
----------------------------------------------
-