[Lazarus] fpWeb and server-sent events

Luca Olivetti luca at wetron.es
Thu Jun 4 15:51:02 CEST 2020


El 4/6/20 a les 15:37, Michael Van Canneyt via lazarus ha escrit:
> 
> 
> On Thu, 4 Jun 2020, Luca Olivetti via lazarus wrote:
> 
>>>      FServer.Port:=8080;
>>>      FServer.Threaded:=true;
>>>      FServer.Run;
>>>    except
>>>      on E:Exception do
>>>      begin
>>>        writeln(E.Message);
>>>        sleep(1000);
>>>        FServer.Free;
>>>      end;
>>>    end;
>>
>> Oh, and I had to do some more overriding so that the response could 
>> have a reference to the calling thread (needed if I want to use 
>> synchronize).
> 
> That seems a bit strange, since you can do synchronize without needing
> access to the thread object using a class method of TThread ?

TThread.Synchronize needs a TThread as the first parameter.
This is my work in progress

https://paste.ubuntu.com/p/cKdQmCQw7p/

The main difference from the previous pastebin is that I subclass 
TFPHTTPConnection to add a TThread field and override the 
CreateConnectionThread to add the thread reference to the connection.
And I added more parameters to the server thread to pass on events from 
the main thread.


Tlogger is a simple class defined in utils.pas (not included) that logs 
messages.

The idea is that every time a new sse connection comes in, I notify the 
main thread (not yet implemented), then in DoSendConnect I enter a loop 
where I get the data from the main thread (with synchronize) then wait 
on an event (that the main thread signals every time the data changes). 
If there is no new data, every 10 seconds I send an empty message as a 
keepalive.


So, I need synchronize:

  - to notify a new connection
  - to get data for that connection
  - to notify that the connection terminated

Bye
-- 
Luca Olivetti
Wetron Automation Technology http://www.wetron.es/
Tel. +34 93 5883004 (Ext.3010)  Fax +34 93 5883007


More information about the lazarus mailing list