2017-03-05 3 views
0

angular2-websocket 랩퍼를 사용하고 있습니다. Websocket을 닫지는 않았지만 나중에 메시지를 보내려고하면 websocket이 이미 닫혔다 고 오류가 발생합니다.명시 적으로 닫지는 않았지만 웹 소켓이 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); 
    } 
    ); 
} 
} 

자바 스크립트 콘솔을 보면 다음과 같은 메시지가 표시됩니다.

 
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

답변

1
을 사용하고 있습니다

나는 angular2-websocket 0.9.0을 사용하고있었습니다. 0.9.1로 업그레이드하면 모든 것이 예상대로 작동합니다. 이 정보가 도움이 되었기를 바랍니다. v0.9.0을 사용하지 마십시오.