我是新来的节点和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();
}
}
请帮助我解决这个问题。
也许你可以解决这个问题:http://stackoverflow.com/questions/3699357/event-calling-before-page-unload – TheHippo