Search code examples
angularwebsocketdartangular-dart

Using WebSockets in Angular Dart


I'm trying to connect a websocket in Dart:

@Injectable()
class Sock {

    ...

    Future<WebSocket> connect() async {
        return WebSocket.connect("ws://....");
    }


}

As soon as I call connect(), I'm seeing an error which seems unrelated:

EXCEPTION: 'dart:io-patch/platform_patch.dart': error: line 25: native function 'Platform_GetVersion' (0 arguments) cannot be found
  static String _version() native "Platform_GetVersion";
  ^

STACKTRACE: 
platform_impl.dart 110                              _Platform.version
platform.dart 73                                    Platform._version
platform.dart 73                                    Platform._version
platform.dart 229                                   Platform.version
http_impl.dart 2843                                 _getHttpVersion
http_impl.dart 1672                                 new _HttpClient
http.dart 1338                                      new HttpClient
websocket_impl.dart 968                             _WebSocketImpl._httpClient
websocket_impl.dart 968                             _WebSocketImpl._httpClient
websocket_impl.dart 994                             _WebSocketImpl.connect
websocket.dart 319                                  WebSocket.connect
package:atomcomponents/util/sock.dart 44:20         Sock.connect
package:atomcomponents/util/sock.dart 35:21         new Sock.<fn>
dart:async                                          _ZoneDelegate.runUnary
package:angular2/src/core/zone/ng_zone.dart 184:21  NgZone._runUnary
dart:async                                          _ZoneDelegate.runUnary
package:angular2/src/core/zone/ng_zone.dart 184:21  NgZone._runUnary



VM276:1 EXCEPTION: NoSuchMethodError: The method 'openUrl' was called on null.
Receiver: null
Tried calling: openUrl("GET", Instance of '_Uri')
STACKTRACE: 
dart:core                                           Object.noSuchMethod
websocket_impl.dart 994                             _WebSocketImpl.connect
websocket.dart 319                                  WebSocket.connect
package:atomcomponents/util/sock.dart 44:20         Sock.connect
package:atomcomponents/util/sock.dart 32:21         new Sock.<fn>
dart:async                                          _ZoneDelegate.runUnary
package:angular2/src/core/zone/ng_zone.dart 184:21  NgZone._runUnary
dart:async                                          _ZoneDelegate.runUnary
package:angular2/src/core/zone/ng_zone.dart 184:21  NgZone._runUnary

Somebody suggested that I add BrowserClient to bootstrap

// bootstrap angular2
bootstrap(AppComponent, [
    ROUTER_PROVIDERS,
    provide(APP_BASE_HREF, useValue: '/'),
    provide(LocationStrategy, useClass: HashLocationStrategy),
    provide(BrowserClient, useFactory: () => new BrowserClient(), deps: [])
]);

This is however not making any difference, WebSocket.connect is still failing with the same enormous stacktrace.

In older code, new WebSocket("ws://..."); used to work, but WebSocket is now an abstract class and that constructor has been deprecated.

  @Deprecated('This constructor will be removed in Dart 2.0. Use `implements`'
      ' instead of `extends` if implementing this abstract class.')
  WebSocket();

The documentation literally only mentions it: https://webdev.dartlang.org/angular/guide/server-communication

The WebSocket protocol is another important communication technology; it isn’t covered in this page.

I'm on Angular 3.1.0 and Dart 1.24.2

Can WebSockets be used with Angular and if so, what is missing to make it work?


Solution

  • Mmmm, seems I was using the backend implementation of Sockets for some reason, changed my import from dart:io and it's working now.

    import 'dart:html';
    

    And this connects:

    new WebSocket("ws://...");