sefidgaran / signalr_client

A Flutter SignalR Client for ASP.NET Core
https://pub.dev/packages/signalr_netcore
MIT License
78 stars 112 forks source link

HubConnectionBuilder not passing Message headers to Server Hub #24

Open clmorales opened 2 years ago

clmorales commented 2 years ago

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 commented 2 years ago

Team any workaround or fix for this?

ostup17 commented 2 years ago

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

clmorales commented 2 years ago

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

ostup17 commented 2 years ago

Can't find headers, how to send them?

clmorales commented 2 years ago

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 commented 2 years ago

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

leoshusar commented 2 years ago

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

v0fbu1vm commented 2 years ago

Is anyone working on this?

v0fbu1vm commented 2 years ago

@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 commented 1 year ago

Any update on this?

eifachmache commented 1 year ago

Really too bad

JonMuc commented 1 year ago

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 commented 11 months ago

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

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

cyberprophet commented 11 months ago
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 commented 11 months ago

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 commented 11 months ago

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?