Skip to content

TWebClientConnector

The TWebClientConnector is a component to establish a connection between a TMS Web Core application running in the browser and a client application written in FMX or VCL running in a desktop or mobile environment. In combination with the TTMSFNCWebCoreClientBrowser (available in TMS FNC Core) the TMS Web Core application can be viewed in your favorite environment. TWebClientConnector is defined in the unit WEBLib.ClientConnector.

Setting up the TWebClientConnector

In your TMS Web Core application, drop an instance of TWebClientConnector on the form. There are no additional steps necessary to start receiving and sending messages at browser side.

To receive messages, you can implement the OnReceivedMessage event. The OnReceivedMessage returns JSON, below is a sample of parsing JSON in the OnReceivedMessage event:

procedure TForm1.DoReceivedMessage(Sender: TObject; AJSON:
TJSONObject);
var
  s: string;
begin
  s := TJSJSON.stringify(AJSON.JSObject);
  WebMemo1.Text := s;
end;

Sending messages with the TWebClientConnector

To send messages, you need to encapsulate your data in JSON, then send it to the client, which the TWebClientConnector is connected to.

procedure TForm1.SendButtonClick(Sender: TObject);
var
  o: TJSONObject;
  js: TJSON;
  s: string;
  I: Integer;
begin
  js := TJSON.Create;
  s := '{"Message From Browser":"'+
TTMSFNCUtils.EscapeString(WebMemo1.Text) +'"}';
  o := js.parse(s);
  w.Send(o);
  o.Free;
end;

Ofcourse, sending and receiving will only work when a client, writing in VCL or FMX, is connected. Below are the steps necessary to have a working connection between browser and client.

Setting up the TTMSFNCWebCoreClientBrowser

Drop an instance of the TTMSFNCWebCoreClientBrowser on the form and enter the URL of your TMS Web Core application. When starting the application, the client will automatically try to establish a connection with the TMS Web Core application running the TWebClientConnector component instance. When the connection is established, the OnConnected event is triggered, allowing you to start sending and receiving messages. For receiving messages at client side, the OnReceivedMessage event (similar to the TMS Web Core application implementation for TWebClientConnector) can be used.

procedure TForm1.DoReceiveMessage(Sender: TObject; AJSON: TJSONValue);
var
  s: String;
begin
  if AJSON.TryGetValue<String>('Message From Browser', s) then
  begin
    ShowMessage(TTMSFNCUtils.UnescapeString(s));
  end;
end;
To send messages to the TMS Web Core application you can use the following code:

procedure TForm1.SendButtonClick(Sender: TObject);
var
  c: TJSONObject;
begin
  c := TJSONObject.Create;
  c.AddPair('Message From Client', 'Hello World !');
  TMSFNCWebCoreClientBrowser1.Send(c);
  c.Free;
end;