eSeGeCe
software
TsgcWebSocketServer is part of the sgcWebSockets suite and provides a high-level component for creating WebSocket servers in C++Builder. This guide walks through creating a minimal server that automatically replies to incoming messages.
TsgcWebSocketServer component on the form (named sgcWSServer1 by default).TMemo (MemoLog) to display activity.Set up the server in the form's OnCreate handler:
void __fastcall TForm1::FormCreate(TObject *Sender)
{
sgcWSServer1->Port = 9001; // listen on port 9001
sgcWSServer1->Active = true; // start the server
}
Handle the OnMessage event to echo the received text back to the client:
void __fastcall TForm1::sgcWSServer1Message(TsgcWSConnection *Connection,
const UnicodeString Text)
{
Connection->WriteData("Server received: " + Text); // reply only to sender
// sgcWSServer1->Broadcast(Text); // alternative: send to all clients
MemoLog->Lines->Add(Text); // optional logging
}
WriteData transmits the response only to the specific connection, whereas Broadcast sends the message to every connected client.
Compile and run the application. Any WebSocket client that connects to ws://<your-host>:9001/ will receive a response prefixed with "Server received:" for every message it sends.
When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.