在 Angular Dart 中使用 WebSocket
Using WebSockets in 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
现在是一个抽象 class 并且该构造函数已被弃用。
@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
The WebSocket protocol is another important communication technology;
it isn’t covered in this page.
我在 Angular 3.1.0
和 Dart 1.24.2
WebSockets 可以与 Angular 一起使用吗?如果可以,还缺少什么才能让它工作?
嗯,似乎出于某种原因我正在使用 Sockets 的后端实现,更改了我从 dart:io
的导入,它现在可以工作了。
import 'dart:html';
这连接:
new WebSocket("ws://...");
我正在尝试在 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
现在是一个抽象 class 并且该构造函数已被弃用。
@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
The WebSocket protocol is another important communication technology; it isn’t covered in this page.
我在 Angular 3.1.0
和 Dart 1.24.2
WebSockets 可以与 Angular 一起使用吗?如果可以,还缺少什么才能让它工作?
嗯,似乎出于某种原因我正在使用 Sockets 的后端实现,更改了我从 dart:io
的导入,它现在可以工作了。
import 'dart:html';
这连接:
new WebSocket("ws://...");