sefidgaran / signalr_client

A Flutter SignalR Client for ASP.NET Core

Home Page:https://pub.dev/packages/signalr_netcore

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HubConnectionBuilder not passing Message headers to Server Hub

clmorales opened this issue · comments

When configuring HubConnectionBuilder().withUrl options headers. The headers are not been pass to the server hub.

My Code:

final headers = MessageHeaders();
headers.setHeaderValue("MyHeader-One", "MY-VALUE1");
headers.setHeaderValue("MyHeader-Two", "MY-VALUE2");

hubConnection = HubConnectionBuilder()
.withUrl(
serverUrl,
options: HttpConnectionOptions(
headers: headers,
transport: HttpTransportType.LongPolling,
),
)
.withAutomaticReconnect()
.build();

Header MyHeader-One and MyHeader-Two are not been received at Hub

Team any workaround or fix for this?

I have a similar problem, were you able to solve it?

No, and end up using signalr_pure, you may try it. Hope it works.

Can't find headers, how to send them?

Can't find headers, how to send them?

Remember this is for the signalr_pure package

  1. Create a HttpConnetionOptions
  2. Set options.headers
  3. Include ...httpConnectionOptions in HubConnectionBuilder

void main() async {
final options = HttpConnectionOptions();
options.headers = {'customHeader1': 'value1', 'customHeader2': 'value2'} ;
final builder = HubConnectionBuilder()
..url = 'url'
..logLevel = LogLevel.information
..httpConnectionOptions = options
..reconnect = true;
final connection = builder.build();
connection.on('send', (args) => print(args));
await connection.startAsync();
await connection.sendAsync('send', ['Hello', 123]);
final obj = await connection.invokeAsync('send', ['Hello', 'World']);
print(obj);
}

Bro, thanks a lot. I spent about 4 days sending headers via signalr_netcore

Here is being created empty MessageHeaders() object instead of using user provided options headers.

Is anyone working on this?

@clmorales

package: cure: ^0.1.0-nullsafety.0

    final HubConnectionBuilder builder = HubConnectionBuilder()
      ..url = _url
      ..logLevel = LogLevel.critical
      ..httpConnectionOptions = HttpConnectionOptions(
        headers: {
          "remote-ip-address": ipAddress ?? "",
          "platform-type": PlatformInfo().platform.name.capitalizeFirstLetter()
        },
      )
      ..reconnect = true;

      _connection = builder.build();

Any update on this?

commented

Really too bad

commented

Is there a solution? I'm trying to send the header but it just won't, I've tried many ways and none of them get the server-side header

The header reaches the server, but it is not recognized during the update process.

Does "X-Requested-With: FlutterHttpClient" not recognize this part?

info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: POST
      Scheme: http
      PathBase:
      Path: /hubs/intro/negotiate
      Host: 192.168.0.2:19456
      User-Agent: Dart/3.1 (dart:io)
      Accept-Encoding: gzip
      Content-Type: text/plain;charset=UTF-8
      Content-Length: 0
      X-Requested-With: FlutterHttpClient
      useridentifier: [Redacted]

The header reaches the server, but it is not recognized during the update process.

Does X-Requested-With: FlutterHttpClient not recognize this part?

info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: GET
      Scheme: http
      PathBase:
      Path: /hubs/intro
      Connection: Upgrade
      Host: 192.168.0.2:19456
      User-Agent: Dart/3.1 (dart:io)
      Accept-Encoding: gzip
      Cache-Control: no-cache
      Upgrade: websocket
      Sec-WebSocket-Version: 13
      Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
      Sec-WebSocket-Key: [Redacted]

WebSocket protocol differs from HTTP/HTTPS by enabling bidirectional data exchange across multiple frames instead of just a single request. A WebSocket connection usually starts with HTTP/HTTPS and then transitions to WebSocket with an upgrade request.

As seen in the provided logs, the indication (Upgrade: websocket) shows that the connection has been upgraded to WebSocket. Subsequently, communication takes place using the WebSocket protocol, where each message is not perceived as a separate HTTP/HTTPS request. Therefore, attempting to find "UserIdentifier" in context.Request.Headers is meaningless.

In WebSocket protocol, a different approach is needed to send custom headers. Typically, custom data is transmitted during connection setup or when sending messages in WebSocket. To include user identification information when setting up a WebSocket connection, you need to add this information on the client side during WebSocket connection establishment. The server can then read and process this information upon WebSocket connection establishment. The specific method may vary depending on the WebSocket library and framework in use. If you can provide additional information about the WebSocket library you are using, it would be helpful.

Can't find headers, how to send them?

Remember this is for the signalr_pure package

  1. Create a HttpConnetionOptions
  2. Set options.headers
  3. Include ...httpConnectionOptions in HubConnectionBuilder

void main() async { final options = HttpConnectionOptions(); options.headers = {'customHeader1': 'value1', 'customHeader2': 'value2'} ; final builder = HubConnectionBuilder() ..url = 'url' ..logLevel = LogLevel.information ..httpConnectionOptions = options ..reconnect = true; final connection = builder.build(); connection.on('send', (args) => print(args)); await connection.startAsync(); await connection.sendAsync('send', ['Hello', 123]); final obj = await connection.invokeAsync('send', ['Hello', 'World']); print(obj); }

Has the header transmission been resolved?