在Angular Dart中使用WebSockets

时间:2017-10-14 15:06:50

标签: angular websocket dart angular-dart

我正在尝试连接Dart中的websocket:

@Injectable()
class Sock {

    ...

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


}

一旦我致电connect(),我就会看到一个似乎无关的错误:

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

有人建议我将BrowserClient添加到bootstrap

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

然而,这没有任何区别,WebSocket.connect仍然失败了同样巨大的堆栈跟踪。

在较旧的代码中,new WebSocket("ws://...");曾经工作,但WebSocket现在是一个抽象类,并且该构造函数已被弃用。

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

文档字面上只提到它: https://webdev.dartlang.org/angular/guide/server-communication

  

WebSocket协议是另一项重要的通信技术;   它不在本页面中。

我在Angular 3.1.0Dart 1.24.2

WebSockets可以和Angular一起使用吗?如果有的话,可以使用什么来使它工作?

1 个答案:

答案 0 :(得分:2)

嗯,似乎我出于某种原因正在使用套接字的后端实现,从dart:io更改了我的导入,现在它正在工作。

import 'dart:html';

这就连接起来了:

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