dart-lang / web_socket_channel

StreamChannel wrappers for WebSockets.
https://pub.dev/packages/web_socket_channel
BSD 3-Clause "New" or "Revised" License
425 stars 112 forks source link

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

Open princesoni18 opened 3 months ago

princesoni18 commented 3 months ago

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

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

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 commented 1 month ago

Use this:


 import 'package:web_socket_channel/io.dart';

      _channel = IOWebSocketChannel.connect(
        url,
        headers: {
          'Authorization': 'Bearer $apiKey',
        },
      );
CarlosFelicioPainkillers commented 1 month ago

Use this:

 import 'package:web_socket_channel/io.dart';

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

Ins't supported by web platform

AndreyDAraya commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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