Skip to content

BitmasterXor/mORMot2-WebSockets-VCL-Components-For-Delphi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌐 mORMot2 WebSocket Components v2.0.5

High-Performance Async WebSocket Components for Delphi

Version Delphi Platform License


🎬 Component Preview

πŸ–₯️ WebSocket Server Component

WebSocket Server Component Preview

πŸ’» WebSocket Client Component

WebSocket Client Component Preview

See the components in action with real-time WebSocket connections, bulletproof encryption, and enterprise-grade performance!


✨ Why Choose mORMot2 WebSocket Components?

🎯 Built for Modern Web

  • mORMot2's WebSocket Framework: Leverages the lightning-fast mORMot2 async WebSocket architecture
  • Thousands of Concurrent Connections: Handle massive WebSocket client loads with ease
  • Standard WebSocket Protocol: Full RFC 6455 compliance with frame-based communication
  • Real-Time Bidirectional: Instant push/pull communication between client and server

πŸ”§ Developer-Friendly

  • Drop-in Components: Familiar Delphi component architecture with events and properties
  • Thread-Safe Design: Bulletproof multi-threaded WebSocket operation
  • Smart Auto-Reconnection: Intelligent reconnection strategies with exponential backoff
  • Coordinated Encryption: Advanced AES encryption with automatic parameter coordination

πŸ›‘οΈ Enterprise Features

  • AES Encryption: Full AES support with automatic mode/key coordination (ECB, CBC, CFB, OFB, CTR, GCM, CFC, OFC, CTC)
  • Connection State Management: Advanced state tracking and statistics
  • Robust Error Handling: Comprehensive error propagation and recovery
  • Professional Logging: Configurable logging levels for debugging and monitoring

πŸ“¦ Quick Installation

πŸ”¨ 3-Step Installation

  1. Build the Package

    Open mORMot2WebSocketComponents.dproj β†’ Build β†’ Install
    
  2. Add Library Path

    Tools β†’ Options β†’ Library Path β†’ Add source folder path
    
  3. Start Building!

    Find components in Tool Palette under "mORMot2 WebSocket"
    

πŸ—οΈ Components Overview

πŸ–₯️ TmORMot2WebSocketServer - WebSocket Server Component

The powerhouse WebSocket server component that accepts and manages multiple simultaneous WebSocket connections with enterprise-grade performance.

πŸ“‹ Key Properties

Property Type Default Description
Active Boolean False ⚑ Start/stop the WebSocket server
Port Integer 8080 πŸ”Œ Server listening port
MaxConnections Integer 1000 πŸ‘₯ Maximum concurrent connections
ServerThreadPoolCount Integer 32 🧡 Async thread pool size
KeepAliveTimeOut Integer 30000 ⏱️ Keep-alive timeout (ms)
WebSocketsURI String "websocket" 🌐 WebSocket endpoint URI
WebSocketsAjax Boolean True πŸ“‘ Enable AJAX fallback
EncryptionEnabled Boolean False πŸ” Enable AES encryption
EncryptionKey String "" πŸ”‘ AES encryption key
EncryptionMode TAESMode amCBC πŸ›‘οΈ AES encryption mode
EncryptionKeySize TAESKeySize aks256 πŸ”’ Key size (128/192/256)

πŸ“Š Statistics Properties (Read-Only)

Property Type Description
ClientCount Integer πŸ‘₯ Current active connections
TotalConnections Int64 πŸ“ˆ Total connections since start
TotalActiveConnections Int64 πŸ”΄ Live connection count
TotalBytesReceived Int64 πŸ“₯ Total bytes received
TotalBytesSent Int64 πŸ“€ Total bytes sent
TotalMessagesReceived Int64 πŸ“¨ Total messages received
TotalMessagesSent Int64 πŸ“€ Total messages sent

πŸŽͺ Events

Event Description
OnClientConnected πŸŽ‰ WebSocket client connected
OnClientDisconnected πŸ‘‹ WebSocket client disconnected
OnDataReceived πŸ“¨ Process incoming WebSocket data
OnDataSent πŸ“€ WebSocket data transmission complete
OnHandleCommand πŸ“‹ Handle WebSocket commands
OnError ❌ Error occurred
OnServerStateChange πŸ”„ Server state changed

πŸ’» TmORMot2WebSocketClient - WebSocket Client Component

The intelligent WebSocket client component with bulletproof auto-reconnection and smart connection strategies.

πŸ“‹ Key Properties

Property Type Default Description
Active Boolean False ⚑ Connect/disconnect
Host String "localhost" 🌐 Server hostname/IP
Port Integer 80 πŸ”Œ Server port
URI String "/" 🌐 WebSocket URI path
Connected Boolean False πŸ”— Connection state
ConnectionTimeout Integer 30000 ⏱️ Connection timeout (ms)
AutoReconnect Boolean False πŸ”„ Enable auto-reconnection
ReconnectStrategy TWebSocketReconnectStrategy rsLinear πŸ“ˆ Reconnection strategy
ReconnectInterval Integer 5000 ⏰ Base reconnection interval (ms)
MaxReconnectAttempts Integer 0 πŸ”’ Maximum reconnection attempts

πŸ”„ Reconnection Strategies

Strategy Description
rsLinear ⏰ Fixed interval reconnection
rsExponential πŸ“ˆ Exponential backoff (smart)

πŸ“Š Statistics Properties (Read-Only)

Property Type Description
ConnectionState TWebSocketConnectionState πŸ”„ Current connection state
ConnectionStateDescription String πŸ“ Human-readable state
TotalBytesReceived Int64 πŸ“₯ Total bytes received
TotalBytesSent Int64 πŸ“€ Total bytes sent
Reconnecting Boolean πŸ”„ Currently reconnecting

πŸŽͺ Events

Event Description
OnConnect πŸŽ‰ Connected to WebSocket server
OnDisconnect πŸ‘‹ Disconnected from server
OnDataReceived πŸ“¨ Process incoming WebSocket data
OnDataSent πŸ“€ WebSocket data sent
OnHandleCommand πŸ“‹ Handle WebSocket commands
OnError ❌ Error occurred
OnStateChange πŸ”„ Connection state changed
OnReconnecting πŸ”„ Attempting reconnection
OnReconnectFailed ❌ Reconnection attempt failed

πŸš€ Quick Start Examples

πŸ–₯️ Start WebSocket Server

// Start server
procedure TServerForm.StartServer;
begin
  mORMot2WebSocketServer1.Port := 8080;
  mORMot2WebSocketServer1.Active := True;
end;

πŸ”Œ Stop WebSocket Server

// Stop server
procedure TServerForm.StopServer;
begin
  mORMot2WebSocketServer1.Active := False;
end;

πŸ’» Connect WebSocket Client

// Connect to server
procedure TClientForm.ConnectToServer;
begin
  mORMot2WebSocketClient1.Host := '127.0.0.1';
  mORMot2WebSocketClient1.Port := 8080;
  mORMot2WebSocketClient1.Connect;
end;

πŸ”Œ Disconnect WebSocket Client

// Disconnect from server
procedure TClientForm.DisconnectFromServer;
begin
  mORMot2WebSocketClient1.Disconnect;
end;

πŸ“€ Send Data Server to Client

// Send to specific client
procedure TServerForm.SendToClient(ClientID: Integer);
var
  Data: TBytes;
begin
  Data := BytesOf('Hello Client!');
  mORMot2WebSocketServer1.SendCommandToClient(ClientID, Data);
end;

πŸ“€ Send Data Client to Server

// Send to server
procedure TClientForm.SendToServer;
var
  Data: TBytes;
begin
  Data := BytesOf('Hello Server!');
  mORMot2WebSocketClient1.SendCommand(Data);
end;

πŸ“‘ Broadcasting Examples

πŸ“’ Broadcast to All Clients

// Broadcast message to all connected clients
procedure TServerForm.BroadcastToAll;
var
  Data: TBytes;
begin
  Data := BytesOf('Message for everyone!');
  mORMot2WebSocketServer1.BroadcastCommand(Data);
end;

πŸ‘₯ Loop Through All Clients

// Get client information (implement via tracking)
procedure TServerForm.ShowAllClients;
begin
  // Total client count
  ShowMessage(Format('Total clients: %d', [mORMot2WebSocketServer1.GetClientCount]));
  
  // Client IP example
  ShowMessage(mORMot2WebSocketServer1.GetClientIP(ClientID));
end;

πŸ” Encryption Examples

πŸ›‘οΈ Server Encryption Setup

// Configure server encryption
procedure TServerForm.SetupEncryption;
begin
  mORMot2WebSocketServer1.EncryptionEnabled := True;
  mORMot2WebSocketServer1.EncryptionKey := 'MySecretKey123!';
  
  // Set encryption mode (fully qualified)
  mORMot2WebSocketServer1.EncryptionMode := mormot2.WebSocket.Server.TAESMode(amCBC);
  
  // Set key size (fully qualified)
  mORMot2WebSocketServer1.EncryptionKeySize := mormot2.WebSocket.Server.TAESKeySize(aks256);
end;

πŸ”‘ Client Encryption Setup

// Configure client encryption (must match server)
procedure TClientForm.SetupEncryption;
begin
  mORMot2WebSocketClient1.EncryptionEnabled := True;
  mORMot2WebSocketClient1.EncryptionKey := 'MySecretKey123!';
  
  // Set encryption mode (fully qualified)
  mORMot2WebSocketClient1.EncryptionMode := mormot2.WebSocket.Client.TAESMode(amCBC);
  
  // Set key size (fully qualified)  
  mORMot2WebSocketClient1.EncryptionKeySize := mormot2.WebSocket.Client.TAESKeySize(aks256);
end;

πŸ”’ Encryption Mode Examples

// Different encryption modes (Server)
mORMot2WebSocketServer1.EncryptionMode := mormot2.WebSocket.Server.TAESMode(amCBC);  // Default
mORMot2WebSocketServer1.EncryptionMode := mormot2.WebSocket.Server.TAESMode(amCFB);  // Stream
mORMot2WebSocketServer1.EncryptionMode := mormot2.WebSocket.Server.TAESMode(amGCM);  // Authenticated

// Different key sizes (Server)
mORMot2WebSocketServer1.EncryptionKeySize := mormot2.WebSocket.Server.TAESKeySize(aks128);  // 128-bit
mORMot2WebSocketServer1.EncryptionKeySize := mormot2.WebSocket.Server.TAESKeySize(aks192);  // 192-bit
mORMot2WebSocketServer1.EncryptionKeySize := mormot2.WebSocket.Server.TAESKeySize(aks256);  // 256-bit

πŸ“¨ Event Handling Examples

πŸŽͺ Server Events

// Handle client connections
procedure TServerForm.mORMot2WebSocketServer1ClientConnected(Sender: TObject; 
  ClientID: Integer);
begin
  ShowMessage(Format('Client %d connected', [ClientID]));
end;

// Handle incoming data
procedure TServerForm.mORMot2WebSocketServer1DataReceived(Sender: TObject; 
  ClientID: Integer; const Data: TBytes);
var
  Message: string;
begin
  Message := StringOf(Data);
  ShowMessage(Format('Received from client %d: %s', [ClientID, Message]));
end;

πŸŽͺ Client Events

// Handle connection
procedure TClientForm.mORMot2WebSocketClient1Connect(Sender: TObject);
begin
  ShowMessage('Connected to server!');
end;

// Handle incoming data
procedure TClientForm.mORMot2WebSocketClient1DataReceived(Sender: TObject; 
  const Data: TBytes);
var
  Message: string;
begin
  Message := StringOf(Data);
  ShowMessage('Received: ' + Message);
end;

πŸ› οΈ WebSocket Protocol Details

πŸ“¦ Message Structure with Encryption Coordination

+----------------+----------------+---------------+------------------+
| Magic (4 bytes)| Mode (4 bytes) | KeySize (4)   | Reserved (4)     |
+----------------+----------------+---------------+------------------+
| 0x4D4F524D     | AES Mode       | AES Key Size  | Future Use       |
+----------------+----------------+---------------+------------------+
|                    Encrypted WebSocket Data                      |
+------------------------------------------------------------------+

πŸ” Encryption Modes Available

Mode Description Use Case
amECB Electronic Codebook Simple, fast
amCBC Cipher Block Chaining Default, secure
amCFB Cipher Feedback Stream-like
amOFB Output Feedback Stream cipher
amCTR Counter Mode Parallel processing
amGCM Galois/Counter Mode Authenticated encryption
amCFC CFC Mode Advanced
amOFC OFC Mode Advanced
amCTC CTC Mode Advanced

πŸŽͺ Complete Working Example

πŸ–₯️ Simple WebSocket Echo Server

unit WebSocketServerForm;

interface

uses
  System.Classes, Vcl.Forms, Vcl.StdCtrls,
  mORMot2.WebSocket.Server;

type
  TForm1 = class(TForm)
    mORMot2WebSocketServer1: TmORMot2WebSocketServer;
    ButtonStart: TButton;
    ButtonStop: TButton;
    Memo1: TMemo;
    
    procedure ButtonStartClick(Sender: TObject);
    procedure ButtonStopClick(Sender: TObject);
    procedure mORMot2WebSocketServer1DataReceived(Sender: TObject; 
      ClientID: Integer; const Data: TBytes);
  end;

implementation

procedure TForm1.ButtonStartClick(Sender: TObject);
begin
  mORMot2WebSocketServer1.Port := 8080;
  mORMot2WebSocketServer1.Active := True;
  Memo1.Lines.Add('Server started');
end;

procedure TForm1.ButtonStopClick(Sender: TObject);
begin
  mORMot2WebSocketServer1.Active := False;
  Memo1.Lines.Add('Server stopped');
end;

procedure TForm1.mORMot2WebSocketServer1DataReceived(Sender: TObject; 
  ClientID: Integer; const Data: TBytes);
var
  Message: string;
  Response: TBytes;
begin
  Message := StringOf(Data);
  Response := BytesOf('Echo: ' + Message);
  mORMot2WebSocketServer1.SendCommandToClient(ClientID, Response);
end;

end.

πŸ† Performance & Compatibility

⚑ Performance Specs

  • Concurrent Connections: 1000+ simultaneous WebSocket clients
  • Real-Time Communication: Ultra-low latency WebSocket frames
  • Memory Usage: Efficient memory management with async connection pooling
  • CPU Usage: Multi-threaded async WebSocket architecture for maximum performance

πŸ”§ Compatibility

  • Delphi Versions: XE2, XE3, XE4, XE5, XE6, XE7, XE8, 10 Seattle, 10.1 Berlin, 10.2 Tokyo, 10.3 Rio, 10.4 Sydney, 11 Alexandria, 12 Athens, 12.2
  • Platforms: Windows 32-bit & 64-bit
  • Frameworks: VCL, Console applications
  • Dependencies: mORMot2 framework (included)

πŸ“‹ Requirements

  • βœ… mORMot2 framework properly installed
  • βœ… Windows platform (32/64-bit)
  • βœ… Delphi XE2 or later

🎯 Why v2.0.5 is Special

πŸ›‘οΈ BULLETPROOF WebSocket Features

  • Coordinated Encryption: Revolutionary encryption parameter coordination between client/server
  • Smart State Management: Comprehensive WebSocket connection state tracking
  • Bulletproof Reconnection: Exponential backoff strategies for network interruptions
  • Memory Leak Prevention: Advanced cleanup procedures prevent WebSocket resource leaks
  • Thread-Safe Operations: All WebSocket operations properly synchronized for multi-threaded safety

πŸ†• Version 2.0.5 Improvements

  • βœ… Fixed encryption coordination with automatic parameter validation
  • βœ… Enhanced WebSocket reconnection with exponential backoff
  • βœ… Bulletproof WebSocket client disconnect handling
  • βœ… Advanced connection state management with human-readable descriptions
  • βœ… Improved error handling and WebSocket event management
  • βœ… Enhanced encryption context management with salt coordination (Just a hardcoded Salt on both client/server) May enhance the way we generate these salts in the future!

πŸ“ž Support & Community

πŸ’¬ Get Help

  • Issues: Report bugs via GitHub Issues
  • Questions: Community support available
  • Contributing: Pull requests welcome!
  • Discord: bitmasterxor

πŸ“œ License

Open Source - Free to distribute and use in commercial and personal projects!


🌐 Ready to Build High-Performance WebSocket Applications?

Download β€’ Install β€’ Build Amazing Real-Time Apps!

Made By BitmasterXor And Friends With ❀️ for the Delphi Community

About

Easy To Use Drag and Drop VCL mORMot2 WebSocket Components (ASYNC Server) and Base WebSocket Client!

Resources

Stars

Watchers

Forks

Languages