By Guest on Wednesday, 15 January 2025
Posted in General
Replies 15
Likes 0
Views 82
Votes 0
I'm testing the newest version of sgcWebSockets in Delphi to try to connect to a socket.io server.

The server I'm connecting to is using this type of auth:
https://socket.io/docs/v4/client-options/#auth

I can't see any documentation that this type of auth is supported. I've tried using TsgcWebSocketClient Authentication.Token, but that doesn't seem to work with this type of auth.
Hello,

It seems this auth requires to pass the token when opening the http request to get a new session, so you should use the event OnHTTPRequest of the Socket.io API, find below how to pass a bearer token example, just replace "your-token" by the correct value


procedure OnHTTPRequest(Sender: TObject; aRequest: TsgcWSSocketIOHTTPRequest);
begin
aRequest.Headers.Add('Authorization: Token your-token');
end;


If doesn't work, and you can provide the server where you must connect please provide more details and I'll check in my machine.

Kind Regards,
Sergio
·
6 days ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi. It's not a part of the headers. It's sent during the CONNECT in the protocol. See the example here under 'with an additional payload':
https://socket.io/docs/v4/socket-io-protocol/#connection-to-a-namespace
·
6 days ago
·
0 Likes
·
0 Votes
·
0 Comments
·
It is possibly also a part of the handshake outside of the header. See info here:
https://socket.io/docs/v4/server-socket-instance/#sockethandshake

Similar to the HandShakeTimestamp you've already implemented.
·
6 days ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello,

The Socket handshake is just an HTTP GET request with some parameters. According the url:

https://socket.io/docs/v4/server-socket-instance/#sockethandshake

The Auth is passed as a payload, but a HTTP GET request hasn't a payload, it's not a POST, so I assume they are referring to pass an Authorization header like in the example I've sent previously. Did you tested it? it doesn't work?

Kind Regards,
Sergio
·
5 days ago
·
0 Likes
·
0 Votes
·
0 Comments
·
No. The auth is in the payload when connecting to the namespace. See info in the socket io code:
https://github.com/socketio/socket.io/blob/main/packages/socket.io-client/lib/socket.ts

/**
* the authentication payload sent when connecting to the Namespace
*/
auth?: { [key: string]: any } | ((cb: (data: object) => void) => void);


/**
* Credentials that are sent when accessing a namespace.
*
* @example
* const socket = io({
* auth: {
* token: "abcd"
* }
* });
*
* // or with a function
* const socket = io({
* auth: (cb) => {
* cb({ token: localStorage.token })
* }
* });
*/
public auth: { [key: string]: any } | ((cb: (data: object) => void) => void);
·
5 days ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi,

I've added a new property "HandShakeAuthToken" to set the Authentication token. Find attached a compiled sample that use this new feature.

Kind Regards,
Sergio
·
5 days ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Ok. It seems to work with the sample. When will the trial version for Delphi be updated so we can test this further?
·
5 days ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Next month will be a new release, but I can compile a trial with this feature if you tell me which is your delphi version.
·
5 days ago
·
0 Likes
·
0 Votes
·
0 Comments
·
We're using Delphi 11.
·
5 days ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Find below the link to download the trial for D11

https://www.esegece.com/download/samples/sgcWebSockets_D11.zip

Kind Regards,
Sergio
·
4 days ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Ok. Thanks. The auth now works as expected. We also need to use query parameters. Is that something that you have support for?

https://socket.io/docs/v4/client-options/#query
·
23 hours ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Use the parameters property, example:


WSSocketIO.SocketIO.Parameters := '&x=42';
·
22 hours ago
·
0 Likes
·
0 Votes
·
0 Comments
·
I'm trying to send a string array and it doesn't seem to work, like this:


const socket = io({
query: {
arr: ['a', 'b']
}
});


Tired with

WSSocketIO.SocketIO.Parameters := '&arr=[''a'',''b'']';



WSSocketIO.SocketIO.Parameters := '&arr=["a","b"]';



WSSocketIO.SocketIO.Parameters := '&arr=%5B%27a%27,%27b%27%5D';


and various other similar combinations, but nothing seem to work
·
4 hours ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi,

Send without "[]" characters


WSSocketIO.SocketIO.Parameters := '&arr=' + sgcEncodeURIComponent('a,b');


sgcEncodeURIComponent function makes uses of the sgcBase_Helpers unit.

Kind Regards,
Sergio
·
4 hours ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Ok. Thanks
·
3 hours ago
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post