Recent

Author Topic: How to add headers and subprotocol to TWebsocketClient?  (Read 1741 times)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 810
    • Blog personal
How to add headers and subprotocol to TWebsocketClient?
« on: January 17, 2024, 04:02:15 pm »
Hello I need connect to a server using websockets. The information about servers says:
Quote
The sequence of steps involved to setup subscription and start listening for
events are:

    Subscribe on resources - Response to this HTTPS request is a list of
    initial events for the subscribed resources along with the location header.
    The value of the location header is the Web socket address.

    Retrieve the Web socket address from the Location header.

    Configure web socket Subprotocol as rws_subscription.

    Append http-session and ABBCX cookie to the web socket request's header.

    Setup secure web socket connection using the value returned from the
    location header.

    Receive events and parse the events.

Thanks

/BlueIcaro

Warfley

  • Hero Member
  • *****
  • Posts: 1850
Re: How to add headers and subprotocol to TWebsocketClient?
« Reply #1 on: January 25, 2024, 09:40:35 pm »
If you are using LazWebsockets, you can simply use the CustomHeaders property. They will be appended to the request.

The implementation of the Subprotocol is then completely up to you. There is currently no framework for adding subprotocols more easiely, but all the "primitives" should be available.

BlueIcaro

  • Hero Member
  • *****
  • Posts: 810
    • Blog personal
Re: How to add headers and subprotocol to TWebsocketClient?
« Reply #2 on: January 27, 2024, 12:27:40 pm »
If you are using LazWebsockets, you can simply use the CustomHeaders property. They will be appended to the request.

The implementation of the Subprotocol is then completely up to you. There is currently no framework for adding subprotocols more easiely, but all the "primitives" should be available.

Hello I tried you library and ir cover everything I need, but I opened a issue: https://github.com/Warfley/LazWebsockets/issues/36
Because I can't stop my program. So I'm studying other alternatives.

/BlueIcaro

Thaddy

  • Hero Member
  • *****
  • Posts: 16358
  • Censorship about opinions does not belong here.
Re: How to add headers and subprotocol to TWebsocketClient?
« Reply #3 on: January 27, 2024, 02:53:49 pm »
That sounds not right: is your program already written as a service/daemon?
Because that is what I would expect for a webserver application.
« Last Edit: January 27, 2024, 03:00:23 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

BlueIcaro

  • Hero Member
  • *****
  • Posts: 810
    • Blog personal
Re: How to add headers and subprotocol to TWebsocketClient?
« Reply #4 on: January 27, 2024, 03:40:30 pm »
That sounds not right: is your program already written as a service/daemon?
Because that is what I would expect for a webserver application.

I wrote about this here: https://forum.lazarus.freepascal.org/index.php/topic,65547.0.html
I read again, and Warffley said that there is way to solve the problem. I'll will try again.

/BlueIcaro

Warfley

  • Hero Member
  • *****
  • Posts: 1850
Re: How to add headers and subprotocol to TWebsocketClient?
« Reply #5 on: January 27, 2024, 04:18:38 pm »
See my comment in the other issue: https://github.com/Warfley/LazWebsockets/issues/32

It's currently a bug, which I had no time to fix.
But there are a few ways around this. First, when you use .Close, you don't need to call .StopReceiveMessageThread before, as close will kill the stream, which then also terminates the ReceiveMessageThread. It should be noted that closing the stream causes an Exception, which will be caught by the Receiver Thread. So when running normally, it will just silently terminate the thread, BUT, when running with the debugger in Lazarus, it will show a popup that an exception occured. Either just click it away (just click continue), or check the checkbox for Lazarus to ignore this exception in the future, as it is not an error, but simply the way how blocking stream operations are implemented

If you need to use StopReceiveMessageThread, without closing the stream (e.g. if you want to switch to manual message handling) you should simply send a "Ping" message afterwards to make sure a message will be returned which causes the receiving loop to stop.
« Last Edit: January 27, 2024, 04:21:53 pm by Warfley »

 

TinyPortal © 2005-2018