SignalR component uses WebSocket as transport to connect to a SignalR server, if this transport is not supported, an error will be raised.
SignalR client component has a property called SignalR where you can set following data:
The client supports sending Text or Binary data.
Hubs API makes it possible to invoke server methods from the client and client methods from the server. The protocol used for persistent connection is not rich enough to allow expressing RPC (remote procedure call) semantics. It does not mean however that the protocol used for hub connections is completely different from the protocol used for persistent connections. Rather, the protocol used for hub connections is mostly an extension of the protocol for persistent connections.
When a client invokes a server method it no longer sends a free-flow string as it was for persistent connections. Instead, it sends a JSON string containing all necessary information needed to invoke the method. Here is a sample message a client would send to invoke a server method:
WriteData('{"H":"chathub","M":"Send","A":["Delphi Client","Test message"],"I":0}');
The payload has the following properties:
I – invocation identifier – allows to match up responses with requests
H – the name of the hub
M – the name of the method
A – arguments (an array, can be empty if the method does not have any parameters)
If the string argument has double quotes replace " by \"
Example: if the argument is {"test":1}, send the argument as {\"test\":1}
WriteData('{"H":"chathub","M":"Send","A":["{\"test\":1}"],"I":0}');
Authentication can be enabled to associate a user with each connection and filter which users can access to resources. Authentication is implemented using Bearer Tokens, client provide an access token and server validates this token and uses it to identify then user.
Currently only Bearer Tokens are supported:
Here, you pass token directly to Signal server (because token has been obtained from another server).
Authentication.Enabled: if active, authorization will be used before a websocket connection is established.
Authentication.Authentication: 2 types of authentication are supported: bearer token or cookies. Both require an external way to get the required values.
BearerToken: token value obtained.
Cookie: set the value of the cookie required.
oSignalR := TsgcWSAPI_Signal.Create(nil);
oSignalR.SignalR.Enabled := True;
oSignalR.SignalR.Authentication := srcBearerToken;
oSignalR.SignalR.BearerToken.Token := 'token here';
The component has the following events:
This event is called when the client connects successfully to the server, this event is raised.
This event is called when the client is disconnected from the server, this event is raised.
This event is called when there is an error in WebSocket connection.
The protocol used for persistent connection is quite simple. Messages sent to the server are just raw strings. There isn’t any specific format they have to be in. Messages sent to the client are more structured. The properties you can find in the message are as follows:
C – message id, present for all non-KeepAlive messages
M – an array containing actual data.
{"C":"d-9B7A6976-B,2|C,2","M":["Welcome!"]}
This event is called when binary data is received from the server.
When a server method is invoked the server returns a confirmation that the invocation has completed by sending the invocation id to the client and – if the method returned a value – the return value, or – if invoking the method failed – the error.
Here are sample results of a server method call:
{"I":"0"}
A server void method whose invocation identifier was "0" completed successfully.
{"I":"0", "R":42}
A server method returning a number whose invocation identifier was "0" completed successfully and returned the value 42.
{"I":"0", "E":"Error occurred"}
This event is raised when a KeepAlive message is received from the server.