0
私はangular2-websocketラッパーを使用しています。私はwebsocketを閉じていませんが、後でメッセージを送信しようとすると、websocketがすでに閉じられているというエラーが表示されます。明示的に閉じていないのに、WebソケットがAngular 2アプリケーションで閉じているのはなぜですか?
私のコンポーネントは次のようになります。
export class ListDataComponent implements OnInit, OnDestroy, AfterViewInit {
ws: $WebSocket;
sub: Subscription;
ngAfterViewInit() { this.initWebSocket(); }
ngOnDestroy() { this.destroyWebSocket(); }
initWebSocket(): void {
this.ws = new $WebSocket('ws://localhost:8080/topic/data');
this.sub = this.ws.getDataStream().subscribe(
data => {
console.log(data);
//this part is following the example on the github page
//makes me unsure because the close method will force a close and true/false only signifies if the closing is immediate or not
//even if i comment out this line, i still get the same behavior
this.ws.close(false);
},
err => console.err(data)
);
}
destroyWebSocket(): void {
//this is the only place where i destroy/release my websocket resources
if (this.sub) {
try {
this.sub.unsubscribe();
} catch(err) { }
}
if (this.ws) {
try {
this.ws.close(true);
} catch(err) { }
}
}
//this method is bound to a bunch of checkboxes that triggers sending data back to the websocket topic
selectionChanged(i: number): void {
let json = getSomeJson(i); //gets some JSON to send back; omitted
this.ws.send(json).subscribe(
data => console.log(data),
err => {
//it seems i can't send because the websocket has been closed! why?
console.error(err);
}
);
}
}
JavaScriptコンソールを見ると、次のメッセージが表示されます。
Socket connection has been closed (anonymous) @ list-data-ws.component.ts:141 SafeSubscriber.__tryOrSetError @ Subscriber.js:243 SafeSubscriber.error @ Subscriber.js:199 Subscriber._error @ Subscriber.js:128 Subscriber.error @ Subscriber.js:102 (anonymous) @ angular2-websocket.js:123 Observable._trySubscribe @ Observable.js:57 Observable.subscribe @ Observable.js:45 webpackJsonp.362.ListTransactionWsComponent.cryptoSelected @ list-transaction-ws.component.ts:139 View_ListTransactionWsComponent1.handleEvent_2 @ /AppModule/ListTransactionWsComponent/component.ngfactory.js:68 (anonymous) @ view.js:664 (anonymous) @ dom_renderer.js:489 webpackJsonp.946.ZoneDelegate.invokeTask @ zone.js:363 onInvokeTask @ ng_zone.js:264 webpackJsonp.946.ZoneDelegate.invokeTask @ zone.js:362 webpackJsonp.946.Zone.runTask @ zone.js:166 ZoneTask.invoke
奇妙なことは、データストリームにデータが戻ってきているということです。私はバックエンド(デバッグメッセージ)をバックエンドに配置しています(Spring Boot)、WebSocketが閉じられているのを確認できません。
- 私のフロントエンドは、角の角度-CLI v1.0.0デベロッパー-beta.32.3と2と
- 私のバックエンドを使用している
それが助け場合は、春ブーツv1.5.1デベロッパー