0

我是 node 和 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();
  }
 }

请帮我解决这个问题。

4

1 回答 1

0

Chrome 推送通知仅在浏览器运行时有效。此外,一旦您关闭所有浏览器窗口,套接字将离线,这就是您收到此响应的原因。

于 2013-05-17T12:10:18.947 回答