Skip to main content
Table of Contents

Getting started with TMS FNC WebSocket Server

Prerequisites

  • TMS FNC Core and TMS FNC WebSocket installed (see Installation).

Add the component

  1. Drop TTMSFNCWebSocketServer from the TMS FNC WebSocket palette page onto a form, or create one at runtime.
  2. Set Port to the port the server should listen on.
  3. Wire OnConnect, OnDisconnect, and OnMessageReceived, then set Active := True to start listening.

The minimal server — port, connection events, and starting it:

Start a WebSocket serverPascalC++
procedure TForm1.StartServer;
begin
  FServer := TTMSFNCWebSocketServer.Create(Self);
  FServer.Port := 5050;              // default is 8888
  FServer.PathName := 'chat';        // clients connect to ws://<host>:5050/chat
  FServer.AutoSyncEvents := True;    // raise events on the main thread

  FServer.OnConnect := ServerConnect;
  FServer.OnDisconnect := ServerDisconnect;
  FServer.OnMessageReceived := ServerMessageReceived;

  FServer.Active := True;            // begin listening for clients
end;

procedure TForm1.StopServer;
begin
  FServer.Active := False;           // stop listening and drop all clients
end;

procedure TForm1.ServerConnect(Sender: TObject; AConnection: TTMSFNCWebSocketConnection);
begin
  // A TCP connection arrived; the WebSocket handshake has not happened yet.
  Log('Connection from ' + AConnection.PeerIP);
end;

procedure TForm1.ServerDisconnect(Sender: TObject; AConnection: TTMSFNCWebSocketConnection);
begin
  Log('A client disconnected');
end;
void __fastcall TForm1::StartServer()
{
  FServer = new TTMSFNCWebSocketServer(this);
  FServer->Port = 5050;              // default is 8888
  FServer->PathName = "chat";        // clients connect to ws://<host>:5050/chat
  FServer->AutoSyncEvents = true;    // raise events on the main thread

  FServer->OnConnect = ServerConnect;
  FServer->OnDisconnect = ServerDisconnect;
  FServer->OnMessageReceived = ServerMessageReceived;

  FServer->Active = true;            // begin listening for clients
}

void __fastcall TForm1::StopServer()
{
  FServer->Active = false;           // stop listening and drop all clients
}

void __fastcall TForm1::ServerConnect(TObject *Sender, TTMSFNCWebSocketConnection *AConnection)
{
  // A TCP connection arrived; the WebSocket handshake has not happened yet.
  Log("Connection from " + AConnection->PeerIP);
}

void __fastcall TForm1::ServerDisconnect(TObject *Sender, TTMSFNCWebSocketConnection *AConnection)
{
  Log("A client disconnected");
}

Next steps

  • Guides — running a server (including TLS) and broadcasting between clients.
  • API reference — full class, property, method, and event reference.