0

我是新来的节点和socket.io。我刚刚通过推送通知完成了Chrome扩展。在这个问题上,当Chrome浏览器关闭时,通知不会发送。服务器通知如下所示,并且通知未发送。节点 - 浏览器关闭时的推送通知

info - transport end (socket end) 
debug - set close timeout for client kgcuzAOgn5-lrnuQI0Qb 
debug - cleared close timeout for client kgcuzAOgn5-lrnuQI0Qb 
debug - cleared heartbeat interval for client kgcuzAOgn5-lrnuQI0Qb 
debug - discarding transport 

这里是我的代码

服务器

io.sockets.on('connection', function (socket) { 
fs.watch('example.xml', function (curr, prev) { 
    fs.readFile('example.xml', function (err, data) { 
    if (err) throw err;  
    parser.parseString(data); 
}); 
}); 

    parser.addListener('end', function(result) { 

    // adding the time of the latest update 
    //result.time = new Date(); 
    socket.volatile.emit('notification' , result); 
    }); 
}); 

客户

var socket = new io.connect('http://website:8000'); 
console.log('client connected'); 

socket.on('notification', function(data){ 
console.log("message recieved1 = "+data); 

    console.log("message recieved = "+data); 
    showNotification(data) 
    chrome.browserAction.setBadgeText({ text : "1" }); 


}); 

function showNotification(data){ 
var havePermission = window.webkitNotifications.checkPermission(); 
    if (havePermission == 0) { 
    // 0 is PERMISSION_ALLOWED  

var notification = webkitNotifications.createNotification(
    'http://website.com/favicon.ico', // icon url - can be relative 
    'Hello!', // notification title 
    data // notification body text 
); 
// Hide the notification after the configured duration. 
    //setTimeout(function(){ notification.cancel(); }, 5000); 

    notification.onclick = function() { 
     window.open("http://website.com/favicon.gif"); 
     notification.close(); 
    resetBadgeText(); 
    } 
    chrome.browserAction.setBadgeText({"text":"1"}); 
    notification.show(); 
    } else { 
     window.webkitNotifications.requestPermission(); 
    } 
} 

请帮助我解决这个问题。

+0

也许你可以解决这个问题:http://stackoverflow.com/questions/3699357/event-calling-before-page-unload – TheHippo

回答

0

Chrome推送通知仅在浏览器运行时才有效。同样,一旦关闭所有浏览器窗口,套接字将脱机,这就是为什么你会得到这个响应。

+0

有什么办法可以在浏览器关闭时发送通知? – user1087188

+0

不,没有办法在浏览器关闭时发送通知。进程或服务应该在后台运行。我不确定你的初始要求。但是,您可以通过创建一个将在后台运行以保持套接字连接处于活动状态的服务来在移动设备上实现此目的。 – user1974500