Tag Archives: FMX

Delphi Websockets

Been compiling a version of the following WebSockets delphi repository: https://github.com/staspiter/delphi-websocket the third 3-WSS. My setup was under Linux compilation using RAD Delphi 10.4.2 FMXLinux GetIt-Addon – on Ubuntu 20.4-LTS Linux. Using IndyProject Components set.

The certain delphi-websocket repository was coded as part of Delphi CodeRage2019.

Clients were standard .js browser side the sent/received messages in an IRC room alike setup..  Just wanted to run a POC on this code.

Thing that caused the code to get ‘Stuck’ was a situation where the end clients were mobile – and

  • got connected via local Wifi
  • manually disconnected from Wifi
  • connected to data connectivity
  • refresh chat page
  • disconnected from data connectivity
  • refresh chat page
  • connected to local Wifi again
  • and refresh the page

When snooping the network using WireShark the following log lines were already raised in the situation described above – xx.xx.xx.xx – is the external Domain (cause most of the connections I made were from the inside LAN to the external Domain IP – Except when connecting the mobile to cell data connection) IP, 192.168.2.194 is the WebSocket Delphi Server local IP.

  • 22900 1126.696488 xx.xx.xx.xx 192.168.2.194 TCP 66 56732 → 8889 [RST, ACK] Seq=1667 Ack=4941 Win=107904 Len=0 TSval=23369304 TSecr=4087275315
  • 22901 1126.697212 192.168.2.194 xx.xx.xx.xx TCP 60 8889 → 50466 [FIN, ACK] Seq=3136 Ack=1 Win=501 Len=0
  • 22902 1126.697226 192.168.2.194 xx.xx.xx.xx TCP 60 8889 → 50462 [FIN, ACK] Seq=3136 Ack=1 Win=501 Len=0
  • 22903 1126.697232 192.168.2.194 xx.xx.xx.xx TCP 60 8889 → 50452 [FIN, ACK] Seq=3136 Ack=103 Win=501 Len=0

What you see in this log, that when the StuckUp-Actions taken. A RST call was made (RST are standard TCP flag calls [RESET] – which are triggered every time an end point disconnects). And this cause a shutdown to all connected clients (you can see the connected clients in the different PeerIP each WebSocket Client get).

When debugging that (using Delphi IDE and PAServer bridge on Linux) Server raised the following error: “Delphi Project raised exception class Broken pipe (13)” – (the other errors such as the SSL error in the image – were probably raised due to communication and server stuck issue). I initiate a chat with Remy Lebeau – who is maintaining the Indy code for a while now.

Took me some time, actually debugging and finding this error. But Remy understood exactly at no time how to fix that. I had to run the following code lines:

uses Posix.Signal;

signal(SIGPIPE, Pointer(SIG_IGN));

In official Delphi environment the namespace units ‘POSIX’ contains different units which handle different Linux RTL API calls.

Disabling the Pipe is done slightly differently on Apple and on Linux platforms machines.

As for the present time (as I write this) WebSockets aren’t integrated into actual Indy components set. There are several open-sourced versions floating around (and of course at leaft four commercial components set versions – which in this case were too much to invest in, just yet). I found the above github repo good enough cause I could use it cross-platform on my Linux.

MQTT Delphi fmx

I was looking for MQTT (Mosquitto protocol implementation) using Indy components (that supports FMX [Delphi Firemonkey framework]).

I found this repo https://github.com/wizinfantry/delphi-mqtt-client — which seems to work after adding the following fix:

On file .\delphi-mqtt-client\LIB\MQTT.pas on function ‘function TMQTT.WriteData(AData: TBytes): boolean;’ using proper TMemoryStream something like that:

function TMQTT.WriteData(AData: TBytes): boolean;
var
  sentData: integer;
  attemptsToWrite: integer;
  Stream: TMemoryStream;
  nCount: Integer;
  pData: TArray<byte>;
begin

// :
// ;

    try
      Stream := TMemoryStream.create;
      nCount := Length(AData) - sentData;
      pData := Copy(AData, sentData - 1, Length(AData) + 1);
      Stream.Write(pData, nCount);
      Stream.Seek(0,0);
      FSocket.IOHandler.Write(Stream, Length(AData) - sentData);
      Stream.Free;
      Result := True;
      FisConnected := true;
    except
      raise;
      Result := False;
      FisConnected := false;
    end;

// :
// ;
end;

Although it is only Alpha version, it does seem to do the basic functionality.

Seems to me this repo is a forked version of https://github.com/jamiei/Delphi-TMQTT2 which is the VCL version using Synapse TCP components.

I ran basic tests on Win, Android and Linux (using Delphi Sydney 10.4.2 with Linux GetIt addon)… all seems to work properly — (Linux had a little free memory issue when subscribing a topic — which required try except — as a fast fix)