RTC Forums
April 25, 2024, 01:34:51 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: How to do stuff after sending repsonse to client?  (Read 3669 times)
jamesinform
RTC License
***
Posts: 4


« on: June 18, 2018, 02:06:41 PM »

I have a simple scenario!

My HTTPServer waits for requests from a client (browser). When a requests comes I want to be able to do the following:

1. Compose the response (e.g. get an HTML file and send the response to the client)
2. After that do some internal stuff (that don't influence the response) on the request that takes time
3. End the server thread.

In my specific case, the client transmits data to the server. The server will receive the data and send the client an HTML page in response, for example with the content "Hey, dear client. Thank you for the data, we have received it and will continue to process it!"
After sending the reply, however, the thread in the server should not be terminated yet. But there I want to process the data directly, for example write it into a data warehouse, which can take a few seconds.

I don't want to write the data to the data warehouse first, and the client has to wait a few seconds, before he gets a response.

How can I do this best with RTC without implementing my own logic or splitting off additional threads?
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: June 18, 2018, 02:29:23 PM »

Option 1 > If you want to WAIT for the Server to send a response to the Client and then execute some additional code, you can place your "additional code" into the "OnResponseDone" event, which will be triggered once the response content has been sent out, but ... please keep in mind that the Server will NOT be able to receive new requests from the same Client using that same connection before you exit the "OnResponseDone" event, because your event will "occupy" the thread responsible for handling that connection.

Option 2 > If you do NOT want to WAIT for the Server to send the Response and you do NOT want to interrupt the sending process, then you can NOT do it without using a separate thread, because the thread responsible for processing the request and preparing the response is also used for sending the response to the Client (this part of the code is implemented asynchronously), but ... you can use the RTC thread pool to execute your custom code from a separate thread while the connection thread continues sending a response.

Once you have finished preparing the response, you can use the TRtcQuickJob component or the PostQuickJob function (check the "rtcThrJobs" unit) to post a new "job" (anonymous functions or method) to be executed from a separate background RTC thread, before you exit the RTC event responsible for connection handling. This allows the Server to continue sending the response you've prepared for the Client (this will be done from the connection thread), while your custom code (implemented as an anonymous function or method) will be executed from a separate independent thread.

If you need any data received from the Client or prepared on the Server to execute your custom code from within the anonymous function or method, make sure that your anonymous function or method does NOT try to access the "Sender" object (or any of its properties or methods) directly, because the "Sender" object (with anything linked to it) will be destroyed automatically when the connection is closed (which can happen at any time after you exit the event).

Logged
jamesinform
RTC License
***
Posts: 4


« Reply #2 on: June 18, 2018, 04:14:28 PM »

Wow, that was a fast response. Exactly what I needed.

Thank you very much and cheers from Germany!
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.022 seconds with 16 queries.