/bird-socket-server

This is a websocket server for Delphi.

Primary LanguagePascalMIT LicenseMIT

Bird Socket Server

This is a websocket server for Delphi.

Delphi Supported Versions Platforms

Prerequisites

[Optional] For ease I recommend using the Boss for installation

  • Boss - Dependency Manager for Delphi

Installation using Boss (dependency manager for Delphi applications)

boss install github.com/mateusvicente100/bird-socket-server

Manual Installation

Add the following folder to your project, in Project > Options > Resource Compiler > Directories and Conditionals > Include file search path

../bird-socket-server/src

Getting Started

You need to use Bird.Socket

uses Bird.Socket;

Create an instance of TBirdSocket and assign the methods and propertys

procedure
var
  LServer: TBirdSocket;
begin
  LServer := TBirdSocket.Create(8080);
  try
    LServer.AddEventListener(TEventType.CONNECT,
      procedure(const ABird: TBirdSocketConnection)
      begin
        // Do on connect.
      end);

    LServer.AddEventListener(TEventType.EXECUTE,
      procedure(const ABird: TBirdSocketConnection)
      begin
        // Do on execute.
      end);

    LServer.AddEventListener(TEventType.DISCONNECT,
      procedure(const ABird: TBirdSocketConnection)
      begin
        // Do on disconnect.
      end);
    LServer.Start;
  finally
    LServer.DisposeOf;
  end;
end;

Samples

The bird socket can run in many Delphi way.

Console - Run a console application

ISAPI - Generate a ISAPI dll for IIS

VCL - Run a VCL application

Service - Run a windows service

Delphi Client

For Delphi websocket connections I recommend to use the bird-socket-client project. It's simple to use and install

procedure Start;
var
  LBirdSocket: TBirdSocketClient;
begin
  LBirdSocket := TBirdSocketClient.New('ws://localhost:8080');

  LBirdSocket.AddEventListener(TEventType.MESSAGE,
    procedure(const AText: string)
    begin
      Log(AText);
    end);

  LBirdSocket.Connect;
  LBirdSocket.Send('Hello Server');
end;

Web Client

For web we have native websocket connection in html

<script>
  const socket = new WebSocket('ws://localhost:8080');

  socket.addEventListener('message', function (event) {
    console.log(event.data);
  });

  socket.addEventListener('open', function (event) {
    socket.send('Hello Server!');
  });
</script>