Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HubConnectionBuilder not passing Message headers to Server Hub #24

Open
clmorales opened this issue Apr 21, 2022 · 16 comments
Open

HubConnectionBuilder not passing Message headers to Server Hub #24

clmorales opened this issue Apr 21, 2022 · 16 comments

Comments

@clmorales
Copy link

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

@ranjanraviraj
Copy link

Team any workaround or fix for this?

@ostup17
Copy link

ostup17 commented Aug 2, 2022

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

@clmorales
Copy link
Author

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

@ostup17
Copy link

ostup17 commented Aug 2, 2022

Can't find headers, how to send them?

@clmorales
Copy link
Author

clmorales commented Aug 2, 2022

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);
}

@ostup17
Copy link

ostup17 commented Aug 3, 2022

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

@leoshusar
Copy link

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

@waadsulaiman
Copy link

Is anyone working on this?

@waadsulaiman
Copy link

waadsulaiman commented Sep 19, 2022

@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();

@NicolasDionB
Copy link

Any update on this?

@eifachmache
Copy link

Really too bad

@JonMuc
Copy link

JonMuc commented Aug 30, 2023

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

@cyberprophet
Copy link

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

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

@cyberprophet
Copy link

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]

@cyberprophet
Copy link

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.

@cyberprophet
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants