2016-06-20 4 views
0

サンプルのコードバス/イオンアプリを開発しています。私はangular2/typescriptを使用しています。私はnode.jsサーバーからのイベントストリームを処理するGETリクエストを発行しました。この接続を終了したいと思います。どうやってやるの?Angular2で作成されたHTTP接続はどのように接続を終了しますか?

ionViewWillEnter(){ 

// Register for SSE Events 
var sseUrl = this.hostUrl + '/api/v1/br/notifications'; 

this.response = this.http.get(sseUrl).map(res => res.json()); 
this.response.subscribe(
    data => { 
     doSomething(data); 
    }, 
    err => console.error(err)); 
} 

ionViewWillLeave(){ 
    // What should I do here?? 
} 

サーバー側のコード下記のようにある:

//API: POST /notifications 
function getNotifications(req, res){ 
    req.socket.setTimeout(0); 
    addListeners(res, notify); 

    //send headers for event-stream connection 
    res.writeHead(200, { 
     'Content-Type': 'text/event-stream', 
     'Cache-Control': 'no-cache', 
     'Connection': 'keep-alive' 
    }); 
    res.write('\n'); 

    req.on("close", function() { 
     console.log("Close called..."); 
     removeListener(res); 
    }); 
} 

function notify(data, notifier){ 
    console.log(util.format('Sending: Data: %s', data)); 
    notifier.res.write('data: ' + data + '\n\n'); // Note the extra newline 
} 

答えて

2

.subscribe()あなたが退会することができますSubscriptionを返します。

this.subscription = this.response.subscribe(
    data => { 
     doSomething(data); 
    }, 
    err => console.error(err)); 
} 

... 

this.subscription.unsubscribe(); 
+0

パーフェクト。ありがとう。 :) – georgekuruvillak

関連する問題