gpt4 book ai didi

angular - 在Angular Dart中使用WebSockets

转载 作者:行者123 更新时间:2023-12-03 02:57:07 25 4
gpt4 key购买 nike

我正在尝试在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

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



我正在使用 Angular 3.1.0Dart 1.24.2
WebSockets可以和Angular一起使用吗?如果可以,要使它正常工作还缺少什么?

最佳答案

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

import 'dart:html';

这连接:
new WebSocket("ws://...");

关于angular - 在Angular Dart中使用WebSockets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46745978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com