I am trying to make a socket io push logs to client UI. Here is the code in angular 2
ngAfterViewInit(){
this.socket=io('http://localhost:9999')
this.socket.on('send-log-data-'+this.labName,function(data){
this.loaddata(data)
}.bind(this))
}
@HostListener('window:beforeunload', ['$event'])
doSomething($event) {
// if(this.hasChanges) $event.returnValue='Your data will be lost!';
this.socket.emit('disconnect',"")
}
}
After looking in to server side i can see on every reload of web page two new connection are getting created and one connection is getting closed.
On First Reload
new connection made
new connection made
Reloading
Reloading
disonnect request came.Disconnecting
On second reload
Reloading
new connection made
new connection made
new connection made
new connection made
Reloading
Reloading
disonnect request came.Disconnecting
disonnect request came.Disconnecting
Server side code as asked is
var Tail=require('tail').Tail
module.exports = {
io_connect_disconnect : (labName,io) => {
io.on('connection',function(socket){
console.log("new connection made")
filename="output.log"
var options= {fromBeginning: true}
tail = new Tail(filename,options)
console.log("new connection made")
tail.on("line", function(data) {
console.log("triggering")
console.log(data)
io.sockets.emit('send-log-data-'+labName,data)
})
socket.on('disconnect',function(){
tail.unwatch()
console.log("disonnect request came.Disconnecting")
socket.disconnect();
})
})
}
}
Can anyone help me with this strange situation ?