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

how can i add headers in Web socket channel in flutter web #379

Open
princesoni18 opened this issue Aug 25, 2024 · 9 comments
Open

how can i add headers in Web socket channel in flutter web #379

princesoni18 opened this issue Aug 25, 2024 · 9 comments

Comments

@princesoni18
Copy link

princesoni18 commented Aug 25, 2024

in mobile app we can use IoWebSocketChannel but its not supported in Flutter web ,

but I am unable to add Headers in WebsocketChannel , can anyone provide a solution

like we use in IO

    return IOWebSocketChannel.connect(
      url,
      protocols: protocols,
      headers: {'Authorization': "Bearer $accessToken"},
    );
@leeyisoft
Copy link

leeyisoft commented Aug 25, 2024

final headers = {
    'cos': cos, // device_type: iso android macos web
    'vsn': appVsn,
    'pkg': packageName,
    'did': deviceId,
    'tz_offset': DateTime.now().timeZoneOffset.inMilliseconds,
    'method': 'sha512',
    // signKeyVsn 告知服务端用哪个签名key 不同设备类型签名不一样
    'sk': globalSignKeyVsn,
    'sign': EncrypterService.sha512("$deviceId|$appVsn|$cos|$packageName", key)
  };
....
      _wsChannel = IOWebSocketChannel.connect(
        url!,
        headers: headers,
        pingInterval: Duration(milliseconds: _heartTimes),
        protocols: protocols,
      );

@princesoni18
Copy link
Author

i think you misunderstood

i am saying IOWebSocketChannel does not supports on flutter web so i have to use WebSocketChannel and it does not provide headers options

import 'package:web_socket_channel/web_socket_channel.dart';
webSocketChannel = WebSocketChannel.connect( {Uri } );

@leeyisoft
Copy link

i think you misunderstood

i am saying IOWebSocketChannel does not supports on flutter web so i have to use WebSocketChannel and it does not provide headers options

import 'package:web_socket_channel/web_socket_channel.dart'; webSocketChannel = WebSocketChannel.connect( {Uri } );

Is that I did not read the question carefully enough, I do not know how to solve your problem, I do not have time to explore the problem, looking forward to the answer

@AndreyDAraya
Copy link

Use this:

 import 'package:web_socket_channel/io.dart';

      _channel = IOWebSocketChannel.connect(
        url,
        headers: {
          'Authorization': 'Bearer $apiKey',
        },
      );

@CarlosFelicioPainkillers

Use this:

 import 'package:web_socket_channel/io.dart';

      _channel = IOWebSocketChannel.connect(
        url,
        headers: {
          'Authorization': 'Bearer $apiKey',
        },
      );

Ins't supported by web platform

@AndreyDAraya
Copy link

AndreyDAraya commented Oct 24, 2024

Ins't supported by web platform

You are right!. Sorry.

Use this package it's working for me:
https://pub.dev/packages?q=websocket_universal

 final isTextSocketConnected = await socketHandler.connect(
          params: SocketOptionalParams(
        headers: {
          'Authorization': 'Bearer $apiKey',
        },
      ));

@CarlosFelicioPainkillers

image

Ins't supported by web platform

You are right!. Sorry.

Use this package it's working for me: https://pub.dev/packages?q=websocket_universal

 final isTextSocketConnected = await socketHandler.connect(
          params: SocketOptionalParams(
        headers: {
          'Authorization': 'Bearer $apiKey',
        },
      ));

@AndreyDAraya
Copy link

image

Ins't supported by web platform

You are right!. Sorry.
Use this package it's working for me: https://pub.dev/packages?q=websocket_universal

 final isTextSocketConnected = await socketHandler.connect(
          params: SocketOptionalParams(
        headers: {
          'Authorization': 'Bearer $apiKey',
        },
      ));

web_socket_channel don't have support to web, use websocket_universal, it is working on Android, iOS, Web.

websocket_universal: ^1.2.5

@dvmatyun
Copy link

Guys, headers work ONLY for IO (that is mobile devices for example). For web you MUST pass header in one of two ways:

  1. transfer it in first message after webSocket connection
  2. transfer it in URL connection as a URL parameter
    (even using websocket_universal package)

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

5 participants