dart-lang / web_socket_channel

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

[WEB] [3.0] Unsupported operation: Platform._version #219

Open mahmoudajawad opened 2 years ago

mahmoudajawad commented 2 years ago

Flutter: 3.0.2, 3.0.3 dart: 2.17.5 OS: windows, macos

With minimal project:

flutter create dart_websocket_channel

Update pubspec.yaml with:

  web_socket_channel: ^2.2.0

Adding the following snippet to main:

// Importing exactly the same libs
import 'package:flutter/material.dart';
import 'package:web_socket_channel/io.dart';

void main() {
  var channel = IOWebSocketChannel.connect(Uri.parse('wss://host.websocket'));
  channel.stream.listen((message) {
    channel.sink.add('received!');
    channel.sink.close(status.goingAway);
  });

  runApp(const MyApp());
}

// ...

The following error is reported when started application for web:

Flutter Web Bootstrap: Programmatic
Error: Unsupported operation: Platform._version
    at Object.throw_ [as throw]
    (http://localhost:52318/dart_sdk.js:5080:11)
    at _Platform._version
    (http://localhost:52318/dart_sdk.js:57563:17)
    at get version [as version]
    (http://localhost:52318/dart_sdk.js:57637:27)
    at get _version (http://localhost:52318/dart_sdk.js:57507:27)
    at desc.get [as _version]
    (http://localhost:52318/dart_sdk.js:5565:17)
    at get version [as version]
    (http://localhost:52318/dart_sdk.js:57476:26)
    at Object._getHttpVersion
    (http://localhost:52318/dart_sdk.js:205704:31)
    at new _http._HttpClient.new
    (http://localhost:52318/dart_sdk.js:200716:28)
    at HttpClient.new (http://localhost:52318/dart_sdk.js:195179:16)
    at get _httpClient (http://localhost:52318/dart_sdk.js:205700:34)
    at desc.get [as _httpClient]
    (http://localhost:52318/dart_sdk.js:5565:17)
    at _WebSocketImpl.connect
    (http://localhost:52318/dart_sdk.js:205347:72)
    at WebSocket.connect
    (http://localhost:52318/dart_sdk.js:204053:35)
    at IOWebSocketChannel.connect
    (http://localhost:52318/packages/web_socket_channel/io.dart.lib.js
    :96:94)
    at main$
    (http://localhost:52318/packages/websocket_dart/main.dart.lib.js:3
    43:41)
    at http://localhost:52318/web_entrypoint.dart.lib.js:34:33
    at Object._checkAndCall
    (http://localhost:52318/dart_sdk.js:5279:16)
    at Object.dcall (http://localhost:52318/dart_sdk.js:5284:17)
    at http://localhost:52318/dart_sdk.js:140291:18
    at Generator.next (<anonymous>)
    at http://localhost:52318/dart_sdk.js:40641:33
    at _RootZone.runUnary
    (http://localhost:52318/dart_sdk.js:40511:59)
    at _FutureListener.thenAwait.handleValue
    (http://localhost:52318/dart_sdk.js:35438:29)
    at handleValueCallback
    (http://localhost:52318/dart_sdk.js:35999:49)
    at _Future._propagateToListeners
    (http://localhost:52318/dart_sdk.js:36037:17)
    at [_completeWithValue]
    (http://localhost:52318/dart_sdk.js:35872:23)
    at async._AsyncCallbackEntry.new.callback
    (http://localhost:52318/dart_sdk.js:35906:35)
    at Object._microtaskLoop
    (http://localhost:52318/dart_sdk.js:40778:13)
    at _startMicrotaskLoop
    (http://localhost:52318/dart_sdk.js:40784:13)
    at http://localhost:52318/dart_sdk.js:36261:9
masus04 commented 2 years ago

Use the following instead:

import 'package:web_socket_channel/web_socket_channel.dart';
// ...
channel = WebSocketChannel.connect(wsUrl);
mahmoudajawad commented 2 years ago

@masus04, interesting! This definitely works, much thanks. I believe the examples need to be updated to use your snippet. I'll contribute that for the next person who faces the same issue as me.

masus04 commented 2 years ago

Already created a PR: #223

Aaron009 commented 2 years ago
//var channel = IOWebSocketChannel.connect(Uri.parse('ws://localhost:1234'));
  final wsUrl = Uri.parse('ws://localhost:1234')
  var channel = WebSocketChannel.connect(wsUrl);
xurc commented 1 year ago

How to get around this problem if custom headers are needed? The WebSocketChannel.connect method does not have a parameter to pass headers as IOWebSocketChannel.connect does.

IOWebSocketChannel.connect(
            "ws://localhost:1234",
            headers: {
              'Authorization': "Bearer $token",  // <-- here is it
            },
            pingInterval: const Duration(seconds: 10),
          );
kateile commented 1 year ago

@xurc I have the same problem. Found anything?

xurc commented 1 year ago

@xurc I have the same problem. Found anything?

I began to transfer the token in the protocols, but this approach requires changes on the server side: in addition to taking the token from an unusual place, you also need to return the token in the Sec-Websocket-Protocol header.

WebSocketChannel.connect(
                  Uri.parse("${_wsConfig['baseUrl']!}$url"),
                  protocols: [jwtToken],
                )