What i have to do i change the destination port based on some data contained in the packet.
Changing the port dynamically can be done in the
OnConnect event, which is fired after a new
TIdTCPClient is created but before it is
Connect()'ed to the target server. The
TIdTCPClient.Port property can be accessed by type-casting the
TIdMappedPortContext.OutboundClient property to
TIdTCPClient.
To determine which port value to assign, you will have to manually read the necessary data from the inbound client, either in the
OnBeforeConnect or
OnConnect event, which are fired before
TIdMappedPortTCP tries to connect the
OutboundClient to the target server.
TIdMappedPortTCP does not begin reading data from the inbound client until after the
OutboundClient is connected.
While until now I had used the peek method to get part of the package and it worked, but now I wanted to try to get it all.
The thing works and I use it for websockets.
That would require handling the complete WebSocket handshake inside of the
OnBeforeConnect or
OnConnect event, and then letting
TIdMappedPortTCP forward the raw bytes of the WebSocket packets back and forth.
I don't think I would recommend using
TIdMappedPortTCP as a WebSocket proxy, though it is possible, I supppose. I would probably use
TIdHTTPServer instead (since the WebSocket handshake is done over HTTP), or even write (or find) a dedicated WebSocket server instead.
Does this component work well with multiple servers?
It is not
intended for that purpose, but it
can be used for that, as evident by the information you already have.
And after that I would like to try the httpproxyservers even if I am fine with IdMappedPortTcp .
TIdHTTPProxyServer will not work as a WebSocket proxy. It only understands very basic HTTP (and it is not a full HTTP implementation at that), and for an HTTP
GET request (which the WebSocket handshake uses) it will close the connection once the HTTP request has been responded to by the target server. So, there is no opportunity to handle the upgrade from HTTP to WebSocket, let alone to forward WebSocket packets back and forth. Also, if the WebSocket client uses the HTTP
CONNECT request to tunnel a TLS-encrypted connection, then the entire WebSocket handshake and subsequent packets will be encrypted, so you can't do anything with that data at all.