This is a websocket server for Delphi.
[Optional]
For ease I recommend using the Boss for installation
- Boss - Dependency Manager for Delphi
boss install github.com/mateusvicente100/bird-socket-server
Add the following folder to your project, in Project > Options > Resource Compiler > Directories and Conditionals > Include file search path
../bird-socket-server/src
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;
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
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;
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>